20220927蚂蚁笔试AK代码(java,开发岗)
第一题
代码:
import java.util.*; public class Q1 { static boolean flag; static HashSet<Integer> pnum; public static void main(String[] args) { pnum = getPnum(); flag = false; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ans[] = new int[n]; for (int i = 0; i < n; i++) { ans[i] = i+1; } order(ans, 0); if(flag) { for (int i = 0; i < n; i++) { System.out.print(ans[i]+" "); } } else { System.out.println(-1); } } public static void order(int ans[], int i) { if(i>=ans.length) { flag = true; } //分别交换 for (int j = i; j < ans.length; j++) { int tmp = ans[j]; ans[j] = ans[i]; ans[i] = tmp; //非质数 if(!pnum.contains(i+1+ans[i])) { order(ans,i+1); } if(flag) return; tmp = ans[j]; ans[j] = ans[i]; ans[i] = tmp; } } public static HashSet<Integer> getPnum(){ HashSet<Integer> set = new HashSet<>(); set.add(2); set.add(3); for(int i = 4; i <= 100000; i++){ boolean flag = true; for (int j = 2; j <= (int)Math.sqrt(i); j++) { if(i%j == 0) { flag = false; break; } } if(flag) set.add(i); } return set; } }
第二题
代码:
import java.util.PriorityQueue; import java.util.Scanner; import java.util.TreeMap; /* 5 2 2 3 1 3 4 6 1 1 1 2 2 3 3 6 1 1 1 1 1 1 1 */ public class Q2{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); TreeMap<Long,Integer> cnt = new TreeMap<>(); for (int i = 0; i < n; i++) { long val = sc.nextInt(); cnt.put(val,cnt.getOrDefault(val,0)+1); } long ans = 0; for (int i = 1; i <= k; i++) { // System.out.println("pos:"+i+"--->开头"); long pos = i; long rest = cnt.getOrDefault(pos,0)>0?cnt.get(pos)-1:0; while(cnt.ceilingKey(pos)!=null) { ans+=rest; pos = pos+k; if(cnt.getOrDefault(pos,0)+rest>1) { rest = cnt.getOrDefault(pos,0)+rest-1; } else { rest = 0; } } // System.out.println(rest); if(rest != 0) { ans += (rest+1)*rest/2; } } System.out.println(ans); } }
第三题
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; /* 3 3 1 2 1 0 2 0 1 2 1 2 1 2 2 2 3 2 3 1 */ public class Q3 { public static void main(String[] args) { int dirs[][] = new int[][] { {0,0},//自己 {1,0},{2,0},//下 {-1,0},{-2,0},//上 {0,-1},{0,-2},//左 {0,1},{0,2}//右 }; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); long map[][] = new long[n][m]; long updateTime[][] = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { map[i][j] = sc.nextInt(); updateTime[i][j] = 1; } } long ans = 0; int q = sc.nextInt(); // printMap(map); for (int i = 0; i < q; i++) { int t = sc.nextInt(); int x = sc.nextInt()-1; int y = sc.nextInt()-1; int h = sc.nextInt(); for(int dir[]:dirs) { int newX = x+dir[0]; int newY = y+dir[1]; if(newX >= 0 && newX < n && newY>=0 && newY<m) { map[newX][newY] += (t - updateTime[newX][newY]); updateTime[newX][newY] = t; if(map[newX][newY]>=h) { map[newX][newY]-=h; ans+=h; } else{ ans+=map[newX][newY]; map[newX][newY] = 0; } } } // System.out.println("\n第"+i+"次除草,位置:["+x+","+y+"],h:"+h+",t:"+t+",ans:"+ans); // printMap(map); } System.out.print(ans); } public static void printMap(long map[][]) { System.out.println("----》地图"); for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.print(map[i][j]+"\t"); } System.out.println(); } System.out.println("=========================="); } }