Problem solving ideas
from[1,2020]Cycle and accumulate all the time.
Code example
public class House plate making { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 2020; i++) { int x = i; while (x > 1){ if(x % 10 == 2) sum++; x /= 10; } } System.out.println(sum); } }
answer
624
Question B: looking for 2020
Problem solving ideas
Define a two-dimensional matrix, traverse each coordinate,
Code example
import java.util.Scanner; public class B Looking for 2020 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=300,cnt=0; char c[][]=new char[n][n]; for(int i=0;i<n;i++) c[i]=sc.next().toCharArray(); int dir[][]={{0,1},{1,0},{1,1}}; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(c[i][j]!='2')continue; for(int d=0;d<dir.length;d++){ if(i+3*dir[d][0]>=n||j+3*dir[d][1]>=n)continue; if(c[i+dir[d][0]][j+dir[d][1]]=='0'&&c[i+2*dir[d][0]][j+2*dir[d][1]]=='2'&&c[i+3*dir[d][0]][j+3*dir[d][1]]=='0'){ cnt++; } } } } System.out.println(cnt); } }
Question C: snake number
Problem solving ideas
First row first column: 1 1 Second row and second column: 5 1 + 1 x 4 Third row and third column: 13 1 + 1 x 4 + 2 x 4 ... This leads to: The first n Line number n Columns: 1 + 1 x 4 + 2 x 4 + ... +(n -1) x 4 = 2n(n-1)+1
Code example##
public class C Serpentine filling number { public static void main(String[] args) { int n = 20; System.out.println("(2 * n * (n - 1) + 1) = " + (2 * n * (n - 1) + 1)); } }
answer:
761
Question D: seven paragraph code
Problem solving ideas
1,yes a,b,c,d,e,f,g Carry out full arrangement, fill with draft paper, and pay attention to the accuracy; 2,(1),Fully arrange the seven segments of the nixie tube (numbers represent the nixie tube fields),0 representative a,1 representative b,And so on) and then arrange all possible of the seven numbers (take from the seven numbers) m(1<=m<=7)List out. (2),Get all the values, and then judge whether the graph formed by each case is connected. If connected, sum++ (3),When arranging, you should pay attention to ensuring that each arrangement must be increasing or decreasing,So as not to repeat, for example: 012,021,210 Wait, they all mean taking it out of the nixie tube ABC This is the case.
Code example
package provincialGames_11_2020_2_JavaB; public class _04_D_Seven segment code { static boolean[][] M;// M is the adjacency matrix static int[] a;// a represents the number of permutations and combinations static int sum = 0;// Final result public static void main(String[] args) { /* * M Is the adjacency matrix, which can be obtained according to the nixie tube image. For example, a and b,a and f can be connected, so it represents 0 and 1, 0 and 5 are connected */ M = new boolean[7][7]; M[0][1] = M[1][0] = M[0][5] = M[5][0] = true; M[1][2] = M[2][1] = M[1][6] = M[6][1] = true; M[2][3] = M[3][2] = M[2][6] = M[6][2] = true; M[4][3] = M[3][4] = true; M[4][5] = M[5][4] = M[4][6] = M[6][4] = true; M[5][6] = M[6][5] = true; a = new int[7]; for (int i = 0; i < a.length; i++) { a[i] = i; } // All possible permutations, deep search for (int n = 1; n <= 7; n++) { dfs(0, n); } System.out.println(sum); } public static void dfs(int k, int n) { if (k == n) { // If there is only one number, it must be true in this case if (n == 1) { sum++; return; } // Judge whether the graph in this case is connected. It uses the union search set method // initialization int[] pre = new int[n]; for (int i = 0; i < pre.length; i++) { pre[i] = i; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Two level for exhaustive case of all edges // If i and j are connected, J joins i // i and j represent nodes, so when merging, it is jion(pre, // i,j). However, whether the nodes represented by I are connected or not depends on whether a[i]] and [a[j] are true in the adjacency matrix M if (M[a[i]][a[j]]) { jion(pre, i, j); } } } // In the end, if all nodes are connected, then all nodes should be the same as nodes. Otherwise, the graph in this case is not connected boolean flag = true; for (int i = 1; i < pre.length; i++) { if (find(pre, 0) != find(pre, i)) { flag = false; break; } } if (flag) { sum++; } return; } // dfs, deep search for (int i = k; i < a.length; i++) { if (k == 0 || a[i] > a[k - 1]) { int t = a[i]; a[i] = a[k]; a[k] = t; dfs(k + 1, n); t = a[i]; a[i] = a[k]; a[k] = t; } } } // Find root node public static int find(int[] pre, int node) { int son = node, temp = 0; // Find root node while (node != pre[node]) { node = pre[node]; } // Path optimization while (son != node) { temp = pre[son]; // Direct follow-up pre[son] = node; // Go up one space son = pre[son]; } return node; } // Two nodes merge public static void jion(int[] pre, int x, int y) { int fx = find(pre, x); int fy = find(pre, y); // The two nodes belong to different graphs and are merged if (fx != fy) { pre[fy] = fx; } } }
Question E: sorting
Problem solving ideas
1. Find rules
The rules I found in bubble sorting are as follows:
Object length compared | Comparison times |
---|---|
1 | 0 |
2 | 1 |
3 | 2 |
4 | 1 + 2 + 3 |
5 | 1 + 2 + 3 + 4 |
... | ... |
n | n(n - 1) / 2 |
2. Calculate the minimum letter sequence length
n * (n - 1) / 2 >= 100
The minimum value of n is 15;
However, when n = 15, it is assumed that the non repeated string is the reverse order with the smallest dictionary order, that is: onmlkjihgfedcba, this string needs to be compared (15 x 14) / 2 = 105 times;
At this time, we need to think in reverse: when the comparison times are fixed, we need to reduce the number of moves. We only need to consider moving the character on the number of digits to be reduced to the front of the string on the basis of the original string to reduce the number of moves. Therefore, move the sixth bit J in the string onmlkjihgfedcba to the front and become jonmlkihgfedcba. Each time j will compare with O, N, m, l and K respectively, but do not exchange. In this way, five exchanges are saved, and a total of 105-5 = 100 exchanges are finally exchanged.
final result
jonmlkihgfedcba
Tips:
- Inversion code of string:
public class Test { public static void main(String[] args) { String s = "abcdefghijklmno"; String rs = new StringBuilder(s).reverse().toString(); System.out.println(rs); } }
- Dictionary order: dictionary order refers to the method of comparing the size of two strings from front to back.
Test question F: score analysis
Problem solving ideas
- Application of Scanner method and cyclic input;
- The calculation method of maximum, minimum and average;
- Formatted output;
Code example
public class F_Performance analysis { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] scores = new int[n]; for (int i = 0; i < n; i++) { scores[i] = scanner.nextInt(); } M(scores); } private static void M(int[] nums){ int max = nums[0]; int min = nums[0]; double sum = 0; int tem; for (int i = 0; i < nums.length; i++) { sum += nums[i]; if (nums[i] > max){ tem = nums[i]; nums[i] = max; max = tem; } if (nums[i] < min){ tem = nums[i]; nums[i] = min; min = tem; } } System.out.println(max); System.out.println(min); System.out.println(String.format("%.2f", sum/nums.length)); } }
Question G: word analysis
Problem solving ideas
- Input the string from the keyboard to store it in the form of characters;
- Combine characters with arrays, convert flexibly, and record the occurrence times of characters.
Code example
public class G_Word analysis { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[] chars = scanner.nextLine().toCharArray(); int[] counts = new int[26]; // Use its default value to count the subsequent letters for (int i = 0; i < chars.length; i++) { counts[chars[i] - 'a']++; // When the same letters appear, they are accumulated and recorded as long as they appear } int val = counts[0]; int index = 0; for (int i = 0; i < counts.length; i++) { if(counts[i] > val){ val = counts[i]; // Get the largest value in the array index = i; } } System.out.println((char) (index + 'a')); System.out.println(val); } }
Question H: Digital triangle
Problem solving ideas:
DP derivation + parity judgment
Sample code
import java.util.Scanner; public class H { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int arr[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ arr[i][j]=scanner.nextInt(); arr[i][j]+=Math.max(arr[i-1][j-1], arr[i-1][j]); } } System.out.println(n%2==1?arr[n][n/2+1]:Math.max(arr[n][n/2], arr[n][n/2+1])); } }
Question I: String score and
Problem solving ideas
Violent solution
Sample code
import java.util.*; public class I { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String string=scanner.next(); char c[]=string.toCharArray(); long ans=0; for(int i=0;i<c.length;i++){ HashSet<Character> set=new HashSet<Character>(); for(int j=i;j<c.length;j++){ set.add(c[j]); ans+=set.size(); } } System.out.println(ans); } }
Question J: decorative beads
Problem solving ideas
. . .
Code example
. . .