网易笔试题
第一题最小数位和,70%
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } //一直加9,直到多了就减去多出来的数 for (int i = 0; i < n; i++) { int num = arr[i]; String tmp = "9"; while (getSum(tmp)<num) { tmp = "9"+tmp; } int cha = getSum(tmp) - num; tmp = (9-cha) + tmp.substring(1); System.out.println(tmp); } } //计算数位和 private static int getSum(String tmp) { int sum = 0; for (int i = 0; i < tmp.length(); i++) { sum += Integer.valueOf(tmp.charAt(i)+""); } return sum; }第二题,翻倍,可以100
public class operate { static int min = Integer.MAX_VALUE; static boolean found = false; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] res = new int[n]; for (int i = 0; i < n; i++) { int A = in.nextInt(); int B = in.nextInt(); int q = in.nextInt(); int p = in.nextInt(); dfs(A,B,q,p,0); res[i] = min; min = Integer.MAX_VALUE; } for (int i = 0; i < n; i++) { System.out.println(res[i]); } } private static void dfs(int A, int B, int q, int p, int c) { if (A >= B) { found = true; min = Math.min(c,min); return; } dfs (A+q,B,q,p,c+1); if (c + 1 > min) return; dfs (A,B,q*p,p,c+1); } }第三题完美的序列,100,不知道为啥暴力也可以100....
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] res = new int[n]; for (int i = 0; i < n; i++) { int length = in.nextInt(); int[] arr = new int[length]; for (int j = 0; j < length; j++) { arr[j] = in.nextInt(); } int max = Integer.MIN_VALUE; int tmpSum = 0; for (int j = 0; j < length; j++) { tmpSum += arr[j]; for (int k = j+1; k < length; k++) { if (arr[k] < tmpSum) { tmpSum = 0; max = Math.max(max,k-j); break; } else { tmpSum += arr[k]; } } } res[i] = max; } for (int i = 0; i < n; i++) { System.out.println(res[i]); } }第四题逆序对,60%,菜狗只会暴力法呜呜呜
//第四题逆序对距离60% public class nixudui { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } long dis = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { if (arr[j] < arr[i]) { dis += (j-i); } } } System.out.println(dis); } }