#A:门牌制作624
解一:
publicstaticvoidmain(String[] args) { intcount=0;for(inti=1;i0) { if(n%10==2) {count++;}n/=10; }}System.out.println(count);}
解二:
publicstaticvoidmain(String[] args) {intcnt=0;for(inti= 1; i<= 2020; i++) {String s=i+"";for(intj= 0; j< s.length(); j++) {if(s.charAt(j)=='2') cnt++;}}System.out.println(cnt);}
#寻找 202016520
public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner = new Scanner(System.in);char[][] ch = new char[300][300];long sum = 0 ;for (int i = 0; i < ch.length; i++) {ch[i] = scanner.nextLine().toCharArray();}for (int i = 0; i < ch.length; i++) {for (int j = 0; j < ch[0].length; j++) {if (j+3<ch[0].length) {if (ch[i][j]=='2' && ch[i][j+1]=='0' &&ch[i][j+2]=='2' &&ch[i][j+3]=='0' ) {sum++;}}if (i+3<ch.length) {if (ch[i][j]=='2' && ch[i+1][j]=='0' &&ch[i+2][j]=='2' &&ch[i+3][j]=='0') {sum++;}}if (i+3<ch.length && j+3<ch[0].length) {if (ch[i][j]=='2' && ch[i+1][j+1]=='0' &&ch[i+2][j+2]=='2' &&ch[i+3][j+3]=='0') {sum++;}}}}System.out.println(sum);//16520}
#C: 蛇形填数761
解题思路:先手写几行就可以发现,第一行第一列的值为1,第二行第二列的值为5.第三行第三列的值为13,第四行第四列的值为25,仔细观察就可以发现第i行第i列的值为 a = a + (i*4)。
publicstaticvoidmain(String[] args) {// TODOAuto-generated method stub intsum= 1; for(inti= 0; i< 20; i++) { sum= sum+ (i* 4);}System.out.println(sum);}
#D:七段码(dfs) 80
解一:
staticint[][] list= newint[][]{{0,1,0,0,0,1,0},{1,0,1,0,0,0,1},{0,1,0,1,0,0,1},{0,0,1,0,1,0,0},{0,0,0,1,0,1,1},{1,0,0,0,1,0,1},{0,1,1,0,1,1,0},};staticSet<Set> set= newHashSet();staticboolean[] booleans= newboolean[7];publicstaticvoidmain(String[] args) {Scanner sc= newScanner(System.in);ArrayList arr= newArrayList();for(inti= 0 ; i< 7 ; i++){booleans[i] = true;arr.add(i);set.add(newHashSet(arr));dfs(arr);booleans[i] = false;arr.remove(arr.size()-1);}System.out.println(set.size());}privatestaticvoiddfs(ArrayList arr) {set.add(newHashSet(arr));intlast= arr.get(arr.size()-1);for(inti= 0 ; i< list[last].length; i++){if(list[last][i] == 1 && !booleans[i]){booleans[i] = true;arr.add(i);dfs(arr);booleans[i] = false;arr.remove(arr.size()-1);}}}
解二:
public static void main(String[] args) {creatGraph();dfs(1);System.out.println(count);}private static void creatGraph() {arr[1][2] = arr[1][6] = 1;arr[2][1] = arr[2][7] = arr[2][3] = 1;arr[3][2] = arr[3][4] = arr[3][7] = 1;arr[4][3] = arr[4][5] = 1;arr[5][4] = arr[5][6] = arr[5][7] = 1;arr[6][1] = arr[6][5] = arr[6][7] = 1;}private static void dfs(int k) {if (k > 7) {check();int q = 0;for (int i = 1; i <= 7; i++) {if (v[i] && res[i] == i) {q++;}}if (q == 1)count++;return;}v[k] = true;dfs(k + 1);v[k] = false;dfs(k + 1);}private static void check() {for (int i = 1; i <= 7; i++) {res[i] = i;}for (int i = 1; i <= 7; i++) {for (int j = 1; j <= 7; j++) {if (arr[i][j] != 0 && v[i] && v[j]) {int fx = find(i), fy = find(j);if (fx != fy) {res[fx] = fy;}}}}}private static int find(int p) {if (res[p] == p) {return p;} else {res[p] = find(res[p]);return res[p];}}
解三:
static ArrayList[] list;static HashSet set = new HashSet();public static void main(String[] args) {init();for(int i=0; i<7; ++i) {vis[0] = i;set.add(1<<i);dfs(1, 1<<i);}System.out.println(set.size());}static int[] vis = new int[7];public static void dfs(int n, int v) {for(int i=0; i<n; ++i) {for(int t : list[vis[i]]) {int p = v|(1<<t);if(!set.contains(p)) {set.add(p);vis[n] = t;dfs(n+1, p);}}}}public static void init() {//存储七位数的关系 list = new ArrayList[7];for(int i=0; i<7; ++i) {list[i] = new ArrayList();}list[0].add(1);list[0].add(5);list[1].add(0);list[1].add(6);list[1].add(2);list[2].add(1);list[2].add(3);list[2].add(6);list[3].add(2);list[3].add(4);list[4].add(3);list[4].add(5);list[4].add(6);list[5].add(0);list[5].add(4);list[5].add(6);list[6].add(1);list[6].add(2);list[6].add(4);list[6].add(5);}
#E:排序jonmlkihgfedcba
——————————————————————–
——————————————————————–
#F:成绩分析(模拟、暴力)
public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();int a,max=0,min=100;double sum=0;for(int i=0;i<n;i++) {a=sc.nextInt();if(maxa) {min=a;}sum+=a;}System.out.println(max);System.out.println(min);System.out.printf("%.2f",sum/n);}
#G:单词分析(字符串)
publicstaticvoidmain(String[] args) {Scanner sc=newScanner(System.in);String str=sc.next();int[]arr=newint[26];inta=0;;charch= 'a';for(inti=0;ia) { a=arr[str.charAt(i)-97]; ch=str.charAt(i);}elseif(arr[str.charAt(i)-97]==a) { if(ch-str.charAt(i)>0) { a=arr[str.charAt(i)-97]; ch=str.charAt(i); }}}System.out.println(ch); System.out.println(a);}
#H: 数字三角形(DP)
publicstaticvoidmain(String[] args) {Scanner scanner= newScanner(System.in);intn=scanner.nextInt();int[][]arr=newint[n+1][n+1];for(inti= 1; i< arr.length; i++) {for(intj= 1; j<= i; j++) {ints=scanner.nextInt();arr[i][j]=s+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]));}}
解二:
publicstaticvoidmain(String[] args) {Scanner scanner= newScanner(System.in);intn= scanner.nextInt();//base case : f(1) = a1int[][] dp= newint[n][n+1];inttemp= scanner.nextInt();dp[0][0] = temp;intmax= Integer.MIN_VALUE;for(inti= 2; i<= n; i++) {for(intj= 0; j max)max= dp[i-1][j];}dp[i-1][n] = max;max= Integer.MIN_VALUE;}System.out.println(dp[n-1][n]);scanner.close();}
#I:子串分值和(规律题吧)
解一:
publicstaticvoidmain(String[] args) {Scanner sc= newScanner(System.in);int[] arr= newint[26];longres= 0;for(inti= 0;i< 26;i++){arr[i] = -1;}String s= sc.nextLine();String[] str= s.split("");intwillIndex= str.length;for(inti= 0;i< str.length;i++){intlastIndex= arr[str[i].charAt(0)-97];res+= (long)(i- lastIndex)*(willIndex- i);arr[str[i].charAt(0)-97] = i;}System.out.println(res);}
解二:
publicstaticvoidmain(String[] args) {Scanner sc= newScanner(System.in);String str= sc.next();int[] nums= newint[26];longres= 0;longn= str.length();str= "0"+str;for(inti= 1; i< str.length(); i++) {res+= (i- nums[str.charAt(i) - 'a']) * (n- i+1);nums[str.charAt(i) - 'a'] = i;}System.out.println(res);}