2022年9月3日京东后端开发第二题 --- 分裂数字
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int max = Integer.MIN_VALUE; int[] ans = new int[n]; for(int i = 0; i<n; i++){ ans[i] = in.nextInt(); max = Math.max(max, ans[i]); } int[] dp = new int[max+1]; int sum = 0; for(int i=0; i < n; i++){ sum += dfs(ans[i], dp); } System.out.println(sum); } public static int dfs(int num, int[] dp){ if(num == 1) return 0; if(dp[num] != 0) return dp[num]; int mid = isPrime(num); if(mid == -1){ dp[num] = dfs(num - 1, dp) + 1; }else{ int left = dfs(mid, dp); int right = dfs(num / mid, dp); dp[num] = left + right + 1; } return dp[num]; } public static int isPrime(int a){ int mid = (int)Math.sqrt(a); for(int i = mid; i >= 2; i--){ if(a % i == 0){ return i; } } return -1; }
代码提交一直是 0%, 小弟斗胆请问一下各位大佬,到底错在哪里了呢?
#京东笔试##java后端开发#