由于蓝桥杯的题,我不知道从怎么复制,就只能粘贴图片了
翻硬币
代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String start = sc.next();char[] starts = start.toCharArray();String end = sc.next();char[] ends = end.toCharArray();int count=0;for (int i = 0; i < starts.length; i++) {if(starts[i]!=ends[i]){starts[i]=starts[i]=='*'?'o':'*';starts[i+1]=starts[i+1]=='*'?'o':'*';count++;}else {continue;}}System.out.println(count);}
}
错误票据
代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();List<Integer> list = new ArrayList<>();while (sc.hasNext()){list.add(sc.nextInt());}Collections.sort(list);int m = 0;int n = 0;for (int i = 1; i < list.size(); i++) {if (list.get(i).equals(list.get(i - 1))) {m = list.get(i);}else if(list.get(i)-list.get(i - 1)==2){n=list.get(i-1)+1;}}System.out.println(n+" "+m);}
}
总结
读取未知个数的数据时,有两种形式
-
public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();List<Integer> list = new ArrayList<>();while (sc.hasNext()){list.add(sc.nextInt());}} } ```
-
public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int N = scanner.nextInt();scanner.nextLine();ArrayList<Integer> record = new ArrayList<>();for (int i = 0; i < N; i++) {for (String s : scanner.nextLine().split(" ")) {record.add(Integer.valueOf(s));}}} } ``
`