// Fibonacci数列的变种,题目中的第二个例子有问题,无视即可 publicstaticvoidmain(String[] args){ java.util.Scanner sc = new java.util.Scanner(System.in); int p = sc.nextInt(); int v = sc.nextInt(); int count = 1; // for a[1] int last = v; // last is a[i-1]
// System.out.println(last); while (last != 1 && last != 0) { last = p % last; if (last == 0) { break; } // System.out.println(last); count++; } System.out.println(count); }
publicstaticvoidmain(String[] args){ java.util.Scanner sc = new java.util.Scanner(System.in); int n = sc.nextInt(); int p = sc.nextInt(); int x = n / p + (n % p == 0 ? 0 : 1); System.out.println(x); }
publicstaticvoidmain(String[] args){ int MAX_INT = 100000 + 10; java.util.Scanner sc = new java.util.Scanner(System.in); int[] weight = newint[10]; for (int i = 0; i < 10; i++) { weight[i] = MAX_INT; } int n = sc.nextInt(); int p; String s; for (int i = 0; i < n; i++) { p = sc.nextInt(); s = sc.next(); // System.out.println(p + "--" + s); if (weight[getIndex(s)] > p) { weight[getIndex(s)] = p; } } int result = MAX_INT; result = Math.min(result, weight[getIndex("ABC")]); result = Math.min(result, (weight[getIndex("AB")] + weight[getIndex("C")])); result = Math.min(result, (weight[getIndex("BC")] + weight[getIndex("A")])); result = Math.min(result, (weight[getIndex("AC")] + weight[getIndex("B")])); result = Math.min(result, (weight[getIndex("A")] + weight[getIndex("B")] + weight[getIndex("C")])); System.out.println(result); } privatestaticintgetIndex(String s){ // A = 1, B = 2, C = 4 // AB = 3, AC = 5, BC = 6 // ABC = 7 if (s == null || s.length() == 0) { return0; } int containA = 0, containB = 0, containC = 0; if (s.contains("A")) { containA = 1; } if (s.contains("B")) { containB = 2; } if (s.contains("C")) { containC = 4; } return (containA + containB + containC); }
第9题
问题描述
给定一个矩阵 M,由 n 行 m 列组成,第 i 行第 j 列值为 M[i][j]。 定义矩阵 M 的重量为矩阵中所有元素的和,几位weight(M) 请找到矩阵左上角的一个子矩阵S(矩阵的前 r 行中的前 c 列组成),使得这个子矩阵的重量的两倍最接近矩阵 M 重量。即 |2 weight(S)-weight(M)| 最小。 如果有多个子矩阵满足条件,请找出面积 r * c 最小的一个。 如果仍然有多个子矩阵满足条件,请找出其中 r 最小的一个。
输入格式
输入第一行包含两个整数 n, m,表示矩阵的大小。 接下来 n 行,每行 m 个整数,表示给定的矩阵M。
输出格式
输出一行,包含两个整数 r, c,表示子矩阵为矩阵 M 的前 r 行中的前 c 列。
样例输入
1 2 3 4
3 4 3 0 1 1 1 0 1 1 1 1 -2 4
样例输出
1
2 3
数据规模和约定
对于 30% 的评测用例,1 <= n, m <= 20, -10 <= M[i][j] <= 10。 对于 50% 的评测用例,1 <= n, m <= 100, -100 <= M[i][j] <= 100。 对于所有评测用例,1 <= n, m <= 1000, -1000 <= M[i][j] <= 1000。
publicstaticvoidmain(String[] args){ java.util.Scanner sc = new java.util.Scanner(System.in); int[][] M = newint[1001][1001]; int[][] weight = newint[1001][1001]; int n, m; n = sc.nextInt(); m = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { M[i][j] = sc.nextInt(); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int jWeight = 0; for (int k = 1; k <= i; k++) { jWeight += M[k][j]; } weight[i][j] = weight[i][j - 1] + jWeight; // System.out.println(weight[i][j] + "---" + jWeight); } } int r = 0, c = 0, flag = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int temp = Math.abs(2 * weight[i][j] - weight[n][m]); if (temp < flag) { flag = temp; r = i; c = j; } elseif (temp == flag && (i * j) < (r * c)) { r = i; c = j; } elseif (temp == flag && (i * j) == (r * c) && (i < r)) { r = i; c = j; } } } System.out.println(r + " " + c); }
publicstaticvoidmain(String[] args){ java.util.Scanner sc = new java.util.Scanner(System.in); int mod = 1000007; int[] array = newint[1000 + 5]; int n = sc.nextInt(); int k = sc.nextInt(); for (int i = 0; i < n; i++) { array[i] = sc.nextInt(); } int[][] dp = newint[n][k + 1]; for (int i = 0; i < n; i++) { dp[i][1] = 1; } for (int j = 2; j <= k; j++) { for (int i = 0; i < n; i++) { int sum = 0; for (int t = 0; t < i; t++) { if (array[t] < array[i]) { sum += dp[t][j - 1]; } } dp[i][j] = sum; } } int count = 0; for (int i = 0; i < n; i++) { count += dp[i][k]; } System.out.println(count); }