拼多多0312笔试
Q1-100%
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] cs = scan.next().toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cs.length; i++) {
int cnt = 0;
while (Character.isDigit(cs[i])) cnt = cnt * 10 + (cs[i++] - '0');
for (int j = 0; j < cnt; j++) sb.append(cs[i]);
}
System.out.println(sb);
}
}
Q2-100%
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt(), n = scan.nextInt();
int[][] array = new int[t][n];
for (int i = 0; i < t; i++) {
int cnt1 = 0, ans = 0;
for (int j = 0; j < n; j++) {
array[i][j] = scan.nextInt();
if (array[i][j] == 1) cnt1++;
}
ans = cnt1 / 2; // 发射第一种可以消除
ans += n - ans * 2; // 剩下全部发射第二种炮弹
System.out.println(ans);
}
}
}
Q3
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
char[][] notes = new char[n][];
for (int i = 0; i < n; i++) {
notes[i] = scan.next().toCharArray();
}
int[] limits = new int[3];
int[] moneys = new int[3];
for (int i = 0; i < 3; i++) {
limits[i] = scan.nextInt();
moneys[i] = scan.nextInt();
}
Character[][] cs = new Character[n][];
for (int i = 0; i < cs.length; i++) {
cs[i] = new Character[notes[i].length];
for (int j = 0; j < notes[i].length; j++) cs[i][j] = notes[i][j];
Arrays.sort(cs[i], (x, y) -> moneys[x - 'A'] - moneys[y - 'A']);
}
PriorityQueue<Character[]> pq = new PriorityQueue<>((x, y) -> x.length == y.length ?
getPriority(moneys, x) - getPriority(moneys, y) : x.length - y.length);
for (Character[] cc : cs) pq.offer(cc);
boolean flag = true;
int cnt = 0;
long total = 0;
while (!pq.isEmpty()) {
Character[] cc = pq.poll();
boolean f = false;
for (char c : cc) {
int idx = c - 'A';
if (limits[idx] > 0) {
limits[idx]--;
cnt++;
total += moneys[idx];
f = true;
break;
}
}
if (!f) flag = false;
}
System.out.println(flag ? "YES" : "NO");
System.out.println(flag ? total : cnt);
}
public static int getPriority(int[] moneys, Character[] cs) {
int t = 0;
for (char c : cs) t += moneys[c - 'A'];
return t;
}
}
Q4-100%
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) array[i] = scan.nextInt();
double[] avgs = new double[n];
double sum = 0.;
for (int i = 0; i < n; i++) {
sum += array[i];
avgs[i] = Math.round(sum / (i + 1));
System.out.print(Math.round(avgs[i]) + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
addNum(array[i]);
System.out.print(Math.round(findMedian()) + " ");
}
}
// 大堆存更小的那一半数
static PriorityQueue<Integer> maxHeap = new PriorityQueue<>((x, y) -> y - x);
// 小堆存更大的那一半数
static PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // 默认最小堆个数大于或等于最大堆
public static void addNum(int num) {
if (maxHeap.si***Heap.size()) {
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());
} else {
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
}
}
public static double findMedian() {
return minHeap.size() == maxHeap.size() ? (maxHeap.peek() + minHeap.peek()) / 2. : minHeap.peek();
}
}
#PDD##拼多多笔试#