2021年蓝桥杯javaB组第二场题目+部分解析
一、 求余 1
请 问 2021 % 20 的 值 是 多 少?
public static void main(String[] args) {
System.out.println(2021%20);
}
二、双阶乘 59375
解一
public static void main(String[] args) {
long n=1;
for(int i=2021;i>0;i-=2) {
n*=i;
if(n>100000) {
n%=100000;
}
}
System.out.println(n);
}
解二
public static void main(String[] args) {
long ans = 1;
for (int i = 3; i <= 2021; i++) {
if (i % 2 == 1) {
ans = ans * i % 100000;
}
}
System.out.println(ans);
}
三 、格点 15698
暴力解 嘿嘿
public static void main(String[] args) {
long sum=0;
for(int i=1;i<=2021;i++) {
for(int j=1;j<=2021;j++) {
if(i*j<=2021) {
sum++;
}
}
}
System.out.println(sum);
}
四、整数分解(剪枝优化、记忆化搜索(dfs)、DP)691677274345
Dfs
static long[][] f = new long[6][2022];
public static void main(String[] args) {
// 5个数凑出2021
for (int i = 0; i < 6; i++) {
Arrays.fill(f[i], -1);
}
System.out.println(dfs(5, 2021));
}
static long dfs(int n, int sum) {
if (f[n][sum] != -1) return f[n][sum];
if (n == 0) {
if (sum == 0) {
return 1;
}
return 0;
}
f[n][sum] = 0;
for (int i = 1; i <= sum; i++) {
f[n][sum] += dfs(n - 1, sum - i);
}
return f[n][sum];
}
剪枝优化
public static void main(String[] args) {
long ans = 0;
for (int i = 1; i <= 2021; i++) {
for (int j = 1; j <= 2021; j++) {
if (i + j >= 2021) {
break;
}
for (int k = 1; k <= 2021; k++) {
int tmp = 2021 - i - j - k;
if (tmp >= 2) {
ans += tmp - 1;
} else {
break;
}
}
}
}
System.out.println(ans);
}
DP
public static void main(String[] args) {
int n = 2021, k = 5;
long[][] dp = new long[k + 1][n + 1];
Arrays.fill(dp[1], 1);
for (int i = 2; i <= k; i++)
for (int j = i; j <= n; j++)
dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
System.out.println(dp[k][n]);
}
五、城邦(最小生成树)4046
有空在写代码,嘿嘿
六、特殊年份
暴力解:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=0;
for(int i=0;i<5;i++) {
int n=sc.nextInt();
if(n/1000%10==n/10%10&&n%10>n/100%10) {
count++;
}
}System.out.println(count);
}
----------------------------------------------------------------
七 、 小平方
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long count=0;
for(int i=1;i<n;i++) {
if((Math.pow(i, 2)%n)<(n/2)){
count++;
}
}
System.out.println(count);
}
八完全平方数(数学定理)
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Double n=(double) sc.nextInt();
int a=0;
for(int i=1;i<1000;i++) {
double number=Math.sqrt(i*n);
if(number%1==0) {
a=i;
break;
}
}
System.out.println(a);
}
九、负载均衡(模拟 + 优先队列(堆))
public static void main(String[] args) { new Dome9().run(); }
void run() {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.readInt(), m = in.readInt();
Queue<Item>[] cpt = new Queue[n + 1];
int[] cptd = new int[n + 1];
for (int i = 1; i <= n; i++) {
cpt[i] = new PriorityQueue();
cptd[i] = in.readInt();
}
while (m-- > 0) {
int a = in.readInt();
int b = in.readInt();
int c = in.readInt();
int d = in.readInt();
while (cpt[b].size() > 0 && cpt[b].peek().c <= a)
cptd[b] += cpt[b].poll().d;
if (cptd[b] >= d) {
cpt[b].offer(new Item(a + c, d));
out.println(cptd[b] -= d);
} else out.println("-1");
}
out.flush();
}
class Item implements Comparable<Item> {
int c, d;
Item(int c, int d) {
this.c = c;
this.d = d;
}
public int compareTo(Item item) { return this.c - item.c; }
}
class InputReader {
BufferedReader reader;
StringTokenizer token;
InputReader(InputStream in) {
this.reader = new BufferedReader(new InputStreamReader(in));
}
String read() {
while (token == null || !token.hasMoreTokens()) {
try {
token = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return token.nextToken();
}
int readInt() { return Integer.parseInt(read()); }
}
十、国际象棋(dfs)
以上有什么bug可以指出来,我只是个菜鸟,请多多指教