True question link:
Completion
Question A: doorplate making (5 points)
Problem Description:
Brief description of ideas:
Solution 1:
The while loop decomposes the number on each bit
code:
public static void main(String[] args) { int ans=0; for(int i=1;i<=2020;++i){ int a=i; while(a!=0){ if(a%10==2)ans++; a/=10; } } System.out.println(ans); }
Solution 2:
String traversal
code:
public static void main(String[] args) { int ans=0; for(int i=1;i<=2020;++i){ String s=String.valueOf(i); if(s.contains("2")){ for(char c:s.toCharArray()){ if(c=='2')ans++; } } } System.out.println(ans); }
Reference results:
624
Question B: looking for 2020 (5 points)
Problem Description:
**2020.txt file Click here **
Brief description of ideas:
Document 2020 Txt has 300 rows and 300 columns
Traverse each point to find the number of current points combined with right 3, lower 3 and oblique lower 3 to 2020
code:
public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new FileReader("2020.txt")); String s; int n=300,k=0; int [][]a=new int[n][n]; while((s=in.readLine())!=null){ for(int i=0;i<n;++i) a[k][i]=s.charAt(i)-'0'; ++k; } // For (int i = 0; I < n; + + I) {/ / directly enter 2020.txt on the console // String s=scanner.next(); // for(int j=0;j<n;++j){ // a[i][j]=s.charAt(j)-'0'; // } // } int ans=0; for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(j<n-3){ if(a[i][j]==2&&a[i][j+1]==0&&a[i][j+2]==2&&a[i][j+3]==0)ans++; } if(i<n-3){ if(a[i][j]==2&&a[i+1][j]==0&&a[i+2][j]==2&&a[i+3][j]==0)ans++; } if(i<n-3&&j<n-3){ if(a[i][j]==2&&a[i+1][j+1]==0&&a[i+2][j+2]==2&&a[i+3][j+3]==0)ans++; } } } System.out.println(ans); }
Reference results:
16520
Question C: snake number (10 points)
Problem Description:
Brief description of ideas:
Solution 1:
Row 20 and column 20 are located in the oblique number row 20 * 2-1 = 39. Find the first and last numbers of row 39 and calculate 742 + (780-742) / 2 = 761.
code:
public static void main(String[] args) { int n=39,a=0,sum=0; for(int i=1;i<=n;++i){ a++;sum+=a; System.out.println(sum); } System.out.println("\n"+((sum-a+1)+a/2)); }
Solution 2:
Simple simulation. Use the two-dimensional array a to store the oblique number of 39 lines.
code:
public static void main(String[] args) { int n=20*2-1; int [][]a=new int[n][n]; int i=0,j=0,cnt=1; for(int k=0;k<n;++k){ if(k%2==0){ while(j<k){ a[i][j]=cnt++;++j;--i; } if(j==k){ a[i][j]=cnt++;++j; } } else{ while(i<k){ a[i][j]=cnt++;++i;--j; } if(i==k){ a[i][j]=cnt++;++i; } } } for(int k=0;k<n;++k){ for(int l=0;l<n-k;++l){ System.out.print(a[k][l]+" "); } System.out.println(); } System.out.println("\n"+a[19][19]); }
Reference results:
761
Question D: seven segment code (10 points)
Problem Description:
Brief description of ideas:
Solution 1:
Enumerate 2n cases.
- Only one tube of light meets the conditions
- If the x light is on and other tubes connected to x are not on, the conditions are not met
- Finally, deduct the disqualification (abde,facd,bcef) when 4 tube lamps are on and 2 tube lamps are connected together
code:
public static void main(String[] args) { int ans=0; for(int a=0;a<2;++a){ for(int b=0;b<2;++b){ for(int c=0;c<2;++c){ for(int d=0;d<2;++d){ for(int e=0;e<2;++e){ for(int f=0;f<2;++f){ for(int g=0;g<2;++g){ int sum=a+b+c+d+e+f+g; if(sum==0)continue; else if(sum==1){ ans++; } else{ int flag=0; if(a==1&&b==0&&f==0)flag=1; if(b==1&&a==0&&g==0&&c==0)flag=1; if(c==1&&b==0&&g==0&&d==0)flag=1; if(d==1&&c==0&&e==0)flag=1; if(e==1&&d==0&&f==0&&g==0)flag=1; if(f==1&&a==0&&e==0&&g==0)flag=1; if(g==1&&f==0&&e==0&&b==0&&c==0)flag=1; if(flag==0)ans++; } } } } } } } } System.out.println(ans-3); //Subtract ABDE, facd and BCEF }
Solution 2:
dfs + joint search set
code:
private static int n=7; private static int ans=0; private static int[][]mp=new int[n][n]; private static int[]v=new int [n]; private static int[]f=new int [n]; private static void un(int a,int b) { mp[a][b]=1;mp[b][a]=1; } private static int find(int a) { if(a!=f[a])f[a]=find(f[a]); return f[a]; } private static void dfs(int c) { if(c==7){ for(int i=0;i<n;++i)f[i]=i; // initialization for(int i=0;i<n;++i){ // Traverse all possible nixie tubes with lights on for(int j=i+1;j<n;++j){ if(mp[i][j]==1&&v[i]==1&&v[j]==1){ // i. J is on the right and I and j are on int a=find(i),b=find(j); if(a!=b)f[b]=a; // Merge two sets } } } int cnt=0; for(int i=0;i<n;++i) if(v[i]==1&&f[i]==i)cnt++; if(cnt==1)ans++; // All lights belong to the same set return; } v[c]=1; // Light up dfs(c+1); v[c]=0; // Lights out dfs(c+1); } public static void main(String[] args) { // 0~6 --> a~g // Connecting accessible edges un(0,1);un(0,5); un(1,2);un(1,6); un(2,6);un(2,3); un(3,4); un(4,5);un(4,6); un(5,6); dfs(0); System.out.println(ans); }
Illustration:
Reference results:
80
Test question E: ranking (15 points)
Problem Description:
Brief description of ideas:
14 nmlkjihgfedcba 91
15 onmlkjihgfedcba 105
The answer should be a string of length 15
Move onmlkjihgfedcba the 6th character to the first position
Get jonmlkihgfedcba and verify it
code:
public static void main(String[] args) { String s=""; for(int i=0;i<20;++i){ // Look at the watch first s=(char)(i+'a')+s; int cnt=count(s); System.out.println((i+1)+" "+s+" "+cnt); } s="jonmlkihgfedcba"; int cnt=count(s); // verification System.out.println(s+" "+cnt); } private static int count(String s) { char []c=s.toCharArray(); int n=c.length,cnt=0; for(int i=0;i<n-1;++i){ // Bubble sorting for(int j=0;j<n-i-1;++j){ if(c[j]>c[j+1]){ char t=c[j]; c[j]=c[j+1]; c[j+1]=t; cnt++; } } } return cnt; }
Reference results:
jonmlkihgfedcba
Program questions
Test question F: score analysis (15 points)
Answer link:
Problem Description:
-
sample input
7 80 92 56 74 88 99 10
-
sample output
99 10 71.29
Brief description of ideas:
Simple simulation
code:
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); double sum=0; int ma=0,mi=100; for(int i=0;i<n;++i){ int a=scanner.nextInt(); ma=Math.max(ma, a); mi=Math.min(mi, a); sum+=a; } System.out.println(ma); System.out.println(mi); System.out.printf("%.2f\n", sum/n); }#include<iostream> using namespace std; int main(){ int n,score,pass=0,excellent=0;cin>>n; for(int i=0;i<n;++i){ cin>>score; if(score>=60)pass++; if(score>=85)excellent++; } cout<<(int)(pass*100.0/n+0.5)<<"%"<<endl; cout<<(int)(excellent*100.0/n+0.5)<<"%"<<endl; return 0; }
Question G: word analysis (20 points)
Answer link:
Problem Description:
-
Sample input 1
lanqiao
-
Sample output 1
a 2
- Sample input 2
longlonglongistoolong
-
Sample output 2
o 6
Brief description of ideas:
Hash table storage character
code:
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s=scanner.next(); HashMap<Character, Integer>mp=new HashMap<>(); for(char c:s.toCharArray()){ mp.put(c, mp.getOrDefault(c, 0)+1); } char a = 0;int cnt=0; for(char c='a';c<='z';c++){ if(cnt<mp.getOrDefault(c, 0)){ cnt=mp.get(c);a=c; } } System.out.println(a); System.out.println(cnt); }
Question H: number triangle (20 points)
Answer link:
Problem Description:
-
sample input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
-
sample output
27
Brief description of ideas:
Go down from the top to the maximum sum of the current point = max (the sum of the values of the upper left point and the sum of the values of the upper right point) + the value of the current point
The difference between the number of times to go down left and the number of times to go down right cannot exceed 1
- Odd rows can only go to the middle of the last row
- Even rows can only go to the middle two numbers of the last row
code:
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int [][]a=new int[n][n]; int [][]sum=new int[n][n]; for(int i=0;i<n;++i){ for(int j=0;j<=i;++j){ a[i][j]=scanner.nextInt(); if(i==0)sum[i][j]=a[i][j]; if(j<i)sum[i][j]=Math.max(sum[i-1][j]+a[i][j], sum[i][j]); if(j>0)sum[i][j]=Math.max(sum[i-1][j-1]+a[i][j], sum[i][j]); } } int ans=sum[n-1][n/2]; if(n%2==0){ ans=Math.max(sum[n-1][n/2-1],ans); } System.out.println(ans); }
Question I: sum of substring scores (25 points)
Answer link:
Problem Description:
-
sample input
ababc
-
sample output
28
-
Example description
Substring f value a 1 ab 2 aba 2 abab 2 ababc 3 b 1 ba 2 bab 2 babc 3 a 1 ab 2 abc 3 b 1 bc 2 c 1
Brief description of ideas:
Use array a to store the last occurrence of 26 letters
Assuming that each interval is the first letter to appear and has a contribution value, the contribution value is the distance from the same letter on the left * the number of letters on the right
Pay attention to long
code:
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a[]=new int[26]; Arrays.fill(a, -1); String s=scanner.next(); int n=s.length(); long ans=0; for(int i=0;i<n;++i){ int b=s.charAt(i)-'a'; ans+=(long)(n-i)*(i-a[b]); a[b]=i; } System.out.println(ans); }
Question J: decorative beads (25 points)
Answer link:
Problem Description:
-
sample input
1 1 2 1 2 1 1 2 2 2 1 1 1 3 3 1 5 1 2 3 5 8 2 4 2 4 8 15 3 2 5 10
-
sample output
20
Brief description of ideas:
No
Welcome everyone to comment in the comment area