第一题
package ccf201803; import java.util.ArrayList; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<>(); int res = 0; int get; do { get = sc.nextInt(); list.add(get); } while (get != 0); int score = 2; for (int i = 0; i < list.size(); i++) { if (i == 0) { if (list.get(i) == 2) { res += 2; } else if (list.get(i) == 1) { res += 1; } else { break; } } else { if (list.get(i) == 0) break; if (list.get(i) == 2) { if (list.get(i - 1) == 1) { res += score; } else { score += 2; res += score; } } else { res += 1; score = 2; } } } System.out.println(res); } }
第二题
package ccf201803; import java.util.Scanner; public class p2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int L = sc.nextInt(); int t = sc.nextInt(); int[][] a = new int[n + 1][2]; for (int i = 1; i <= n; i++) { a[i][0] = sc.nextInt(); a[i][1] = 1; } for (int i = 0; i < t; i++) { for (int j = 1; j <= n; j++) { if (a[j][0] == L || a[j][0] == 0) { a[j][1] = a[j][1] * (-1); } int k=j+1; if(k==n){ a[j][0] += a[j][1]; }else { for (; k <= n; k++) { if (a[j][0] == a[k][0]) { a[j][1] = a[j][1] * (-1); a[k][1] = a[k][1] * (-1); } } a[j][0] += a[j][1]; } } } for (int i = 1; i <= n; i++) { System.out.print(a[i][0]+" "); } } }