2.8AC--PDD拼多多笔试4.10
第一题AC
思路是把数组从大到小排,或者图方便直接用从小到大快排,然后从大到小遍历,每3个跳过
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
sc.close();
Arrays.sort(nums);
long res = 0;
int curr = 0;
for (int i = n - 1; i >= 0; i--) {
curr++;
if(curr % 3 == 0) continue;
res = res + nums[i];
}
System.out.println(res);
} 第二题0.6
尝试了暴力、尝试了dp都超时/超空间
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
sc.close();
long res = 0;
for (int i = 1; i <= n; i++) {
long curr = 0;
for (int j = 0; j < i; j++) {
curr = curr + nums[j];
}
if(curr % m == 0) res = res + 1;
for (int k = i; k < n; k++) {
curr = curr - nums[k - i] + nums[k];
curr = curr % m;
if(curr == 0) res = res + 1;
}
}
System.out.println(res);
} 第三题AC
牛客原题找靓号,前两天刚做过。。代码略
第四题0.2
毫无思路,直接返回了最多的数字
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
sc.close();
HashMap<Integer, Integer> blocks = new HashMap<>();
for (int i = 0; i < n; i++) {
if(blocks.containsKey(nums[i])) blocks.put(nums[i], blocks.get(nums[i]) + 1);
else blocks.put(nums[i], 1);
}
int res = 0;
for(Integer key : blocks.keySet()){
if(blocks.get(key) > res) res = blocks.get(key);
}
System.out.println(res);
} 今天pdd的全靠第三题前两天做过,不然要被虐哭
#拼多多笔试##拼多多##笔试题目#