标签:java
题目一 输入两个字符串,输出匹配的次数,如 aaaac和aa 输出2
没想到这么水,正则表达式也能过 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s1; String s2; int count=0; while (sc.hasNext()) { count=0; s1=sc.next(); s2=sc.next(); Matcher matcher=Pattern.compile(s2).matcher(s1); while (matcher.find()) { count++; } System.out.println(count); } } }
题目二 有一列由逗号分隔的数字,排序后输出,输出要求,连续的只输出开头和结尾的数字,其他情况全部输出
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class CopyOfCopyOfMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1; while (sc.hasNext()) { s1 = sc.next(); String[] nums = s1.split(","); List<Integer> list = new ArrayList<>(); for (String string : nums) { list.add(Integer.parseInt(string)); } if (list.size() == 1) { System.out.println(list.get(0)); continue; } Collections.sort(list); System.out.print(list.get(0)); for (int i = 1; i < list.size() - 1; i++) { if (list.get(i) - list.get(i - 1) == 1 && list.get(i + 1) - list.get(i) == 1) { } else { System.out.print(" "); System.out.print(list.get(i)); } } System.out.println(" " + list.get(list.size() - 1)); } } }
题目三 大数减法
这道题最水,java一句话搞定,c++的话只能自己实现了 import java.math.BigInteger; import java.util.Scanner; public class CopyOfCopyOfMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1; while (sc.hasNext()) { s1 = sc.nextLine(); String nums[]=s1.split(" +"); System.out.println(new BigInteger(nums[1]).subtract(new BigInteger(nums[0]))); } } }
半小时全部完成,一次通过
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:java
原文地址:http://blog.csdn.net/qingchunweiliang/article/details/47705017