一、填空题
试题A:组队
简单题,不做过多解析,直接上代码
枚举代码:
import java.util.Scanner; public class A{ static int ans=0,sum=0; static int[][] a= {{0,0,0,0,0,0},{0,97,90,0,0,0},{0,92,85,96,0,0},{0,0,0,0,0,93},{0,0,0,0,80,86}, {0,89,83,97,0,0},{0,82,86,0,0,0},{0,0,0,0,87,90},{0,0,97,96,0,0}, {0,0,0,89,0,0},{0,95,99,0,0,0},{0,0,0,96,97,0},{0,0,0,0,93,98}, {0,94,91,0,0,0},{0,0,83,87,0,0},{0,0,0,98,97,98},{0,0,0,0,93,86}, {0,98,83,99,98,81},{0,93,87,92,96,98},{0,0,0,0,89,92},{0,0,99,96,95,81}}; public static void main(String[] args) { Scanner sc=new Scanner(System.in); int i,j,k,l,m; for(i=1;i<=20;i++) for(j=1;j<=20;j++) for(k=1;k<=20;k++) for(l=1;l<=20;l++) for(m=1;m<=20;m++) if(i!=j&&i!=k&&i!=l&&i!=m&&j!=k&&j!=l&&j!=m&&k!=l&&k!=m&&l!=m) { sum=a[i][1]+a[j][2]+a[k][3]+a[l][4]+a[m][5]; ans=Math.max(ans,sum); } System.out.print(ans); } }
搜索代码(这里搜索实际上跟枚举没啥区别):
import java.util.Scanner; public class A { static int ans; static int[] visited=new int[21]; static int[][] a= {{0,0,0,0,0,0},{0,97,90,0,0,0},{0,92,85,96,0,0},{0,0,0,0,0,93},{0,0,0,0,80,86}, {0,89,83,97,0,0},{0,82,86,0,0,0},{0,0,0,0,87,90},{0,0,97,96,0,0}, {0,0,0,89,0,0},{0,95,99,0,0,0},{0,0,0,96,97,0},{0,0,0,0,93,98}, {0,94,91,0,0,0},{0,0,83,87,0,0},{0,0,0,98,97,98},{0,0,0,0,93,86}, {0,98,83,99,98,81},{0,93,87,92,96,98},{0,0,0,0,89,92},{0,0,99,96,95,81}}; public static void dfs(int n,int s) { if(n==6) { ans=Math.max(ans,s); return; } int i; for(i=1;i<=20;i++) if(visited[i]==0) { visited[i]=1; dfs(n+1,s+a[i][n]); visited[i]=0; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); dfs(1,0); System.out.print(ans); } }
答案:490
试题B:不同子串
解析:注意最后一句,只算本质不同的串的个数,在java中我们可以利用HashSet的去重性在存储各子串(通过循环变量i和j,利用String类的substring方法选出子串去存储到Set中),最后统计HashSet的大小输出即可。
上代码:
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.next(); Set<String> set=new HashSet<>(); int i,j; for(i=0;i<s.length();i++) for(j=i;j<s.length();j++) set.add(s.substring(i, j+1)); System.out.println(set.size()); } }
答案:100
试题C:数列求值
解析:此题有一个陷阱,那就是不能一直算下去,会数据溢出。
由于只求最后四位数字那么我们计算后四位即可,把每次相加的结果取余10000存起来。
上代码:
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int i; int[] f=new int[20190325]; f[1]=f[2]=f[3]=1; for(i=4;i<=20190324;i++) f[i]=(f[i-1]+f[i-2]+f[i-3])%10000; System.out.print(f[20190324]); } }
答案:4659
试题D:数的分解
解析:这个题没啥难度,不过有一个小坑,通过三个循环变量i,j,k枚举三个正整数,最后统计出来的结果需要除以6而不是3,要除的是三个数的排列顺序一共是6个而不是三个循环变量的数目!!!
上代码:
import java.util.Scanner; public class D { static int ans=0; public static boolean Is(int x) { String s=String.valueOf(x); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='2'||s.charAt(i)=='4') return false; } return true; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int i,j,k; for(i=1;i<=2019;i++) for(j=1;j<=2019-i;j++) for(k=1;k<=2019-i-j;k++) { if(i+j+k==2019&&Is(i)&&Is(j)&&Is(k)&&i!=j&&i!=k&&j!=k) ans++; } ans=ans/6; System.out.print(ans); } }
答案:40785
试题E:迷宫
代码实现(BFS)
import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; public class lq迷宫 { final static int n=30,m=50; static int[][] map,lu; static int[] dx={1,0,0,-1},dy={0,-1,1,0}; static Queue<pair> q; static pair cur; public static void Print(){ StringBuffer sb=new StringBuffer(); int a=n,b=m,k; while(true){ k=lu[a][b]; switch(k){ case -1: sb.reverse(); System.out.println(sb); System.out.println(sb.length()); return; case 0: sb.append("D"); break; case 1: sb.append("L"); break; case 2: sb.append("R"); break; case 3: sb.append("U"); break; } a-=dx[k]; b-=dy[k]; } } public static void bfs(int x,int y,int d){ q=new LinkedList<>(); map[x][y]=1; //标记为走过 q.add(new pair(x,y,d)); //入队 int tx,ty,i; while(!q.isEmpty()){ cur=q.remove(); //出队 x=cur.x; y=cur.y; d=cur.d; lu[x][y]=d; //保存路径(这个点由哪个方向来的) if(x==n&&y==m){ return; } for(i=0;i<4;i++){ tx=x+dx[i]; ty=y+dy[i]; if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&map[tx][ty]==0){ map[tx][ty]=1; //标记为走过 q.add(new pair(tx,ty,i)); //入队 } } } } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int i,j; String str=new String(); map=new int[n+1][m+1]; lu=new int[n+1][m+1]; for(i=1;i<=n;i++){ str=sc.next(); for(j=1;j<=m;j++) map[i][j]=str.charAt(j-1)-'0'; } bfs(1,1,-1); Print(); } static class pair{ int x,y,d; public pair(){ } public pair(int a,int b,int c){ this.x=a; this.y=b; this.d=c; } } }
答案: DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
试题F:特别数的和
代码
import java.util.*; public class F { static String str; static int sum=0; public static boolean Is(int x) { str=String.valueOf(x); for(int i=0;i<str.length();i++) if(str.charAt(i)=='2'||str.charAt(i)=='0'||str.charAt(i)=='1'||str.charAt(i)=='9') return true; return false; } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int i,n=sc.nextInt(); for(i=1;i<=n;i++) { if(Is(i)) sum+=i; } System.out.print(sum); } }