卡片
原题链接
问题描述
解题思路
从 1 开始拼,1 是最先被用到的,换句话说 1 是最先用完的,如果拼完当前数字后,1 的数量 < 0
了,那么这个数字肯定拼不出来。
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
int cnt = 2021;
for (int i = 1; ; i++) {
int t = i;
while (t > 0) {
cnt -= t % 10 == 1? 1: 0;
t /= 10;
}
if (cnt < 0) {
out.println(i - 1);
break;
}
}
out.flush();
in.close();
}
}
路径
原题链接
问题描述
解题思路
前置知识:最小公倍数/最大公约数/dijkstra求单源最短路
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int INF = 0x3f3f3f3f;
static int N = 2050;
static int[] dis = new int[N];
static boolean[] st = new boolean[N];
static List<List<int[]>> g = new ArrayList<>();
public static int gcd(int a, int b) {
return b == 0? a: gcd(b, a % b);
}
public static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
public static void dijkstra() {
Arrays.fill(dis, INF);
dis[1] = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[1] - b[1]));
pq.offer(new int[] {1, 0});
while (!pq.isEmpty()) {
int[] t = pq.poll();
if (st[t[0]]) continue;
st[t[0]] = true;
for (int[] next: g.get(t[0])) {
if (dis[t[0]] + next[1] < dis[next[0]]) {
dis[next[0]] = dis[t[0]] + next[1];
pq.offer(new int[] {next[0], dis[next[0]]});
}
}
}
out.println(dis[2021]);
}
public static void main(String[] args) throws Exception {
for (int i = 0; i <= 2021; i++) g.add(new ArrayList<>());
for (int i = 1; i <= 2021; i++) {
int ed = Math.min(2021, i + 21);
for (int j = i + 1; j <= ed; j++) {
int d = lcm(i, j);
g.get(i).add(new int[] {j, d});
g.get(j).add(new int[] {i, d});
}
}
dijkstra();
out.flush();
in.close();
}
}
字符统计
原题链接
问题描述
解题思路
数组计数即可,维护最大的出现次数,没什么好说的。
参考代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
int mx = 0;
int[] cnt = new int[26];
String s = in.readLine();
for (char c: s.toCharArray()) {
cnt[c - 'A']++;
mx = Math.max(mx, cnt[c - 'A']);
}
for (int i = 0; i < 26; i++) {
if (cnt[i] == mx) out.print((char)(i + 'A'));
}
out.flush();
in.close();
}
}
费用报销
原题链接
问题描述
解题思路
01背包问题变种 详细题解可以看 lh 学长的这篇题解
参考代码
todo:这里是参考代码