試題 A: 門牌製做
這題,感受還行。java
個人答案:624優化
public class Main1 { public static void main(String[] args) { int n = 2020, sum = 0; for (int i = 1; i <= 1000; i++) { for (int j = 0, k = 1; j < String.valueOf(i).length(); j++, k *= 10) { int m = i % (k * 10) / k; if (m == 2) { sum ++; } } } System.out.println(sum); } }
試題 B: 尋找 2020
這題就是錄入數據的時候有點麻煩,思路挺清晰的,先一行一行找,再一列一列找,最後找斜線的,就是注意控制下標越界。spa
個人答案:16520指針
public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), sum = 0; int[][] a = new int[n][n]; sc.nextLine(); for (int i = 0; i < n; i++) { String s = sc.nextLine(); String[] str = s.split(""); for (int j = 0; j < n; j++) { a[i][j] = Integer.parseInt(str[j]); } } // 行和列 for (int i = 0; i < n; i++) { for (int j = 0; j < n - 3; j++) { int a1 = a[i][j] * 1000 + a[i][j+1] * 100 + a[i][j+2] * 10 + a[i][j+3]; if (a1 == 2020) { sum ++; } int a2 = a[j][i] * 1000 + a[j+1][i] * 100 + a[j+2][i] * 10 + a[j+3][i]; if (a2 == 2020) { sum ++; } } } // 斜線 for (int i = 0; i < n-3; i++) { for (int j = 0; j < n - 3; j++) { int a1 = a[i][j] * 1000 + a[i+1][j+1] * 100 + a[i+2][j+2] * 10 + a[i+3][j+3]; if (a1 == 2020) { sum ++; } } } System.out.println(sum); } }
試題 C: 蛇形填數
這題仔細觀察仍是有規律的,我下面的代碼是找左上到右下斜線上的數。code
個人答案:761排序
public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), sum = 0; for (int i = 1; i <= n + n - 1; i++) { sum += i; } System.out.println(sum - (n - 1)); } }
試題 D: 七段碼
這題題目看錯,聽說答案是:80。string
試題 E: 排序
這題我有點懵,我也算了個答案出來:mnlkojihgfedcba,不知對不對,我感受不妙。it
試題 F: 成績分析
這題,怎麼說呢,感受…沒什麼感受。class
public class Main6 { static int n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } f(a); } static void f(int[] a) { int max = a[0], min = a[0]; double sum = 0.0; for (int i = 0; i < n; i++) { if (max < a[i]) max = a[i]; if (min > a[i]) min = a[i]; sum += a[i]; } System.out.println(max); System.out.println(min); System.out.printf("%.2f", sum / n); } }
試題 G: 單詞分析
這道題思路清晰,個人是這麼想的,雙指針遍歷,右指針定位,左指針查找前面的並計算個數,若個數超過前面的就替換。遍歷
public class Main7 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); f(s); } static void f(String s) { if (s.length() == 1) { System.out.println(s); System.out.println(s.length()); } char m = s.charAt(0); int count = 1; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == m) { count ++; } else { int num = 1; for (int j = 0; j < i; j++) { if (s.charAt(j) == s.charAt(i)) { num ++; } } if (num > count) { m = s.charAt(i); count = num; } } } System.out.println(m); System.out.println(count); } }
試題 H: 數字三角形
這題,我想哭死了,看錯題目,只算出每次查找最大的,沒注意看到「左右向下次數的差不超過1」,20分直接沒了。思路就dfs吧,額。。感受dfs每一年都會考,就控制往下和往右下走,記錄往下走和往右下走的次數,在回溯的時候減一。
public class Main8 { private static int N, sum = 0, l = 0, r = 0; private static int[][] a, xy = { { 1, 0}, { 1, 1}}; private static int[] b; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); a = new int[N][N]; b = new int[N]; for (int i = 0; i < N; i++) { String[] s = sc.nextLine().split(" "); for (int j = 0; j < i + 1; j++) { a[i][j] = Integer.parseInt(s[j]); } } f(0, 0, 0); System.out.println(sum); } static void f(int x, int y, int index) { if (index == N - 1) { if (Math.abs(l - r) <= 1) { sum = sumF(b); } return; } for (int i = 0; i < xy.length; i++) { int o = x + xy[i][0]; int p = y + xy[i][1]; if (o < N && p < N) { if (i == 0) l ++; else r ++; b[index] = a[o][p]; f(o, p, 1 + index); if (i == 0) l --; else r --; } } } static int sumF(int[] arrb) { int sum1 = a[0][0]; for (int i = 0; i < N - 1; i++) { sum1 += arrb[i]; } if (sum1 > sum) sum = sum1; return sum; } }
試題 I: 子串分值和
這題作出來了,可是時間複雜度有點高n^3,這裏就麻煩廣大的網友們幫我優化一下了。
public class Main9 { static Set<String> set = new HashSet<String>(); static int sum = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { sum += num(s.substring(i, j + 1)); } } System.out.println(sum); } static int num(String str) { set.clear(); for (int i = 0; i < str.length(); i++) { set.add(str.substring(i, i + 1)); } return set.size(); } }
試題 J: 裝飾珠
放棄