package com.company.pdd;

import java.util.Scanner;

public class pdd01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int cnt = 0;
StringBuilder res = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(Character.isDigit(c)) {
cnt = cnt * 10 + (int) (c - '0');
continue;
}
res.append(convert(cnt, c));
cnt = 0;
}
System.out.println(res.toString());
}
public static String convert(int n, char c) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(c);
}
return sb.toString();
}
}

第一题无脑循环


第二题数一个数,11才用A否则都用B。

package com.company.pdd;

import java.util.Scanner;

public class pdd01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int cnt = 0;
StringBuilder res = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(Character.isDigit(c)) {
cnt = cnt * 10 + (int) (c - '0');
continue;
}
res.append(convert(cnt, c));
cnt = 0;
}
System.out.println(res.toString());
}
public static String convert(int n, char c) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(c);
}
return sb.toString();
}
}

第三题我优化了一下再深搜,70%,没想到什么好方法能过。大佬求教

package com.company.pdd;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

public class pdd03 {
static int n;
static int res = Integer.MAX_VALUE;
static int[][] limit_cost;
static int[] cnt_num = new int[3];
static int max_res = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
sc.nextLine();
String[] ss = new String[n];
for(int i = 0; i < ss.length; i++) {
ss[i] = sc.nextLine();
}
Arrays.sort(ss, (a, b)->a.length() - b.length());
limit_cost = new int[3][2];
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
limit_cost[i][j] = sc.nextInt();
}
}
int[][] dp = new int[n][3];
for(int i = 0; i < n; i++) {
String s = ss[i];
for(int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
dp[i][c - 'A'] = limit_cost[c - 'A'][1];
}
}
dfs(dp, 0, 0, 0);
if(res == Integer.MAX_VALUE) {
System.out.println("NO");
System.out.println(max_res);
} else {
System.out.println("YES");
System.out.println(res);
}

}
public static void dfs(int[][] dp, int dep, int cost, int max_person) {
if(dep == n && max_person == n) {
res = Math.min(cost, res);
return ;
}
if(dep == n) {
max_res = Math.max(max_res, max_person);
return ;
}
int flag = 0;
for(int j = 0; j < 3; j++) {
if(dp[dep][j] != 0 && cnt_num[j] < limit_cost[j][0]) {
if(flag == 0) max_person++;
flag = 1;
cnt_num[j]++;
cost += limit_cost[j][1];
dfs(dp, dep + 1, cost, max_person);
cnt_num[j]--;
}
}
if(flag == 0) {
dfs(dp, dep + 1, cost, max_person);
}
}
}

第四题两个堆解决战斗

package com.company.pdd;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class pdd04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double sum = 0;
int[] avg = new int[n];
int[] mid = new int[n];
PriorityQueue<Double> min = new PriorityQueue<>();
PriorityQueue<Double> max = new PriorityQueue<Double>(new Comparator<Double>() {
@Override
public int compare(Double o1, Double o2) {
return (o2 - o1) > 0? 1 : -1;
}
});
for(int i = 0; i < n; i++) {
double t = sc.nextDouble();
sum += t;
avg[i] = getNum(sum / (i * 1.0 + 1));
if(i == 0) {
max.add(t);
mid[i] = (int) t;
continue;
}
max.add(t);
min.add(max.poll());
if(max.size() - min.size() > 1) {
min.add(max.poll());
}
if(min.size() - max.size() > 1) {
max.add(min.poll());
}
if(i % 2 == 0) {
mid[i] = i == 0 ? getNum(max.peek()) : getNum(min.peek());
} else {
mid[i] = getNum((min.peek() + max.peek()) / 2);
}
}
for(int i = 0; i < n; i++) {
System.out.print(avg[i] + " ");
}
System.out.println();
for(int i = 0; i < n; i++) {
System.out.print(mid[i] + " ");
}
System.out.println();
}
public static int getNum(double x) {
int y = (int) (x * 10);
y %= 10;
if(y >= 5) {
return (int) Math.ceil(x);
}
return (int) Math.floor(x);
}
}