网易4.16笔试
第一题很简单送分的
#网易笔试##笔试题目##笔经#
第二题先计算最大值和最小值,之后整除余数可以填补与最小值的差距即可,o(n)复杂度,但是只过了93%不知道为什么
public static void wy2(Scanner in){ long n = in.nextLong(); long k = in.nextLong(); long x = in.nextLong(); if(k < n){ System.out.println(-1); return; } long min = 0; for (int i = 1; i <= n; i++) { min += i; } long max = 0; for (int i = 0; i < n ; i++) { max += (k - i); } if(x > max || x < min){ System.out.println(-1); return; } if(n == 1){ System.out.println(x); return; } long flag = 0; long temp = (int) n; long sum = min; if(n == k){ flag = 0; temp = 0; }else{ flag = (x - min)/(k - n); temp = (x - min)%(k - n); } for (int i = 0; i < n - flag - 1; i++) { System.out.print(i + 1); System.out.print(" "); } for (int i = 0; i < flag; i++) { System.out.print(k - i); System.out.print(" "); } if(x != max){ System.out.println(n - flag + temp); } }先写第四题,后来网崩了我就直接提交了不想做了,出去完了
第四题我只想到了n方的方法,一直超时,有无nlogn的想法呢,网崩心也崩,直接没了哈哈哈
public static void wy3(Scanner in){ int n = in.nextInt(); long[][] nums = new long[n + 1][2]; for (int i = 1; i <= n; i++) { int num = in.nextInt(); int temp = num; nums[i][0] = nums[i - 1][0]; nums[i][1] = nums[i - 1][1]; while(temp % 2 == 0){ nums[i][0]++; temp = temp/2; } while(temp%5 == 0){ nums[i][1]++; temp /= 5; } } long res = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <=n ; j++) { res += Math.min(nums[j][0] - nums[i - 1][0], nums[j][1] - nums[i - 1][1]); } } System.out.println(res); }