本文主要是介绍2024 蓝桥打卡Day40,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2021、2022年蓝桥杯真题练习
- 2021年蓝桥杯真题练习
- A ASC
- B 卡片
- C 查找
- D 货物摆放
- F 时间显示
- H 杨辉三角形
- 2022年蓝桥杯真题练习
- A 星期计算
- B 山
- C 字符统计
- D 最少刷题数
- E 求阶乘
2021年蓝桥杯真题练习
A ASC
package THL_0412;public class A_2021 {public static void main(String[] args) {System.out.println((int)'L');}}
B 卡片
package THL_0412;import java.util.Arrays;
import java.util.Iterator;public class B_2021 {public static void main(String[] args) {int[] count = new int[10];Arrays.fill(count, 2021);int temp = -1;for (int i = 1; i < 20000; i++) {String s = i + "";char[] c = s.toCharArray();for (int j = 0; j < c.length; j++) {int num = c[j] - '0';count[num]--;if(count[num]==0) {temp = 0;}}if(temp == 0) {System.out.println(i);break;}}}}
C 查找
package THL_0412;import java.util.HashSet;
import java.util.Set;public class C_2021 {public static void main(String[] args) {Set<String> ans = new HashSet<>();for (int x1 = 0; x1 <= 19; x1++) {for (int y1 = 0; y1 <= 20; y1++) {for (int x2 = 0; x2 <= 19; x2++) {for (int y2 = 0; y2 <= 20; y2++) {if(x1==x2||y1==y2) {continue; }StringBuilder s = new StringBuilder();int up = y2 - y1;int down = x2 - x1;int r = gcd(up,down);s.append( up/r +" ");s.append( down/r +" ");up = y1 * down - x1 * up; // 将k = up/down 代入y1 = k*x1+b 化简得此等式r = gcd(up,down);s.append( up/r +" ");s.append( down/r);ans.add(s.toString());}}}}System.out.println(ans.size()+20+21);}private static int gcd(int a, int b) {if(b == 0) {return a;}else {return gcd( b , a % b);}}
}
D 货物摆放
package THL_0412;import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;public class D_2021 {public static void main(String[] args) {long num = 2021041820210418L;int ans = 0;Set<Long> l = new HashSet<>();for (long i = 1; i < Math.sqrt(num); i++) {if(num % i == 0) {l.add(i);l.add(num/i);}}int count = 0;Long[] nums = l.toArray(new Long[l.size()]);for (int i = 0; i < nums.length; i++) {for (int j = 0; j < nums.length; j++) {for (int k = 0; k < nums.length; k++) {if(nums[i]*nums[j]*nums[k] == num) {count++;}}}}System.out.println(count);}
}
F 时间显示
package THL_0412;import java.util.Scanner;public class F_2021 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);long n = sc.nextLong();n = n /1000; // 1s = 1000ms// 1min = 60s // 1h = 60min = 3600sint h = 0;int min = 0;int s = 0;h = (int) ((n % ( 3600 * 24 ))/3600);min = (int) ((n % 3600)/60);s = (int) (n % 60); System.out.printf("%02d:%02d:%02d",h,min,s); }
}
H 杨辉三角形
package THL_0412;import java.util.*;public class H_2021 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);long N = sc.nextLong();long[] a = new long[44725]; // 创建一个数组来存储杨辉三角的值,长度足够大以覆盖超过十亿的情况a[0] = 1L; // 初始化第一个元素为1if (N == 1) { // 当输入为1时,直接输出1System.out.println(1);return;}int count = 1; // 用于计算当前行的行数for (int i = 1; i < 44725; i++) { // 从第二行开始填充数组for (int j = i; j >= 1; j--) { // 从后往前填充当前行的元素a[j] = a[j] + a[j - 1]; // 按照杨辉三角的规律,每个数等于它上方两个数之和if (a[j] == N) { // 如果找到了输入的N,计算并输出位置int position = (count + 1) * count / 2 + i - j + 1; // 位置计算:前面的个数 + 当前行的位置数System.out.println(position); // 输出位置return;}}count++; // 当前行填充完毕,行数+1}// 当N未在数组中出现时,输出N在未显示出来的第二列的位置上// 未显示的第二列的第一个元素是第一行的最后一个元素加上1// 后续元素是前一个元素与当前行的第一个元素的和long secondColumnFirst = a[1] + 1; // 未显示的第二列的第一个元素long secondColumnElement = secondColumnFirst; // 初始化为第一个元素for (int i = 1; i < N; i++) {secondColumnElement += i + 2; // 每个后续元素是前一个元素加上递增的值}System.out.println(secondColumnElement); // 输出N在未显示的第二列的位置上的值}
}
(2021年能做出来65分,再加上骗分,应该有七十多)
2022年蓝桥杯真题练习
A 星期计算
package THL_0412;public class A_2022 {public static void main(String[] args) {int n = 20;for (int i = 1; i < 22; i++) {n = (20 * n) % 7;}int num = 6 + n;if(num > 7) {num = num % 7;System.out.println(num);}else {System.out.println(num);}}
}
B 山
package THL_0412;public class B_2022 {public static void main(String[] args) {long l = 2022222022;int count = 0;for (int i = 2022; i <= l; i++) {if(isUp(i)&&isMirror(i)) {count++;}}System.out.println(count);}private static boolean isMirror(int i) {StringBuilder s = new StringBuilder(i+"");if(s.toString().equals(s.reverse().toString())) {return true;}else {return false;}}private static boolean isUp(int i) {String s = i + "";int size = s.length() % 2 == 0 ? s.length() / 2 : s.length() / 2 + 1;for (int j = 1; j < size; j++) {if (s.charAt(j) < s.charAt(j - 1)) {return false;}}return true;}
}
C 字符统计
package THL_0412;import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;public class C_2022 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.nextLine();char[] c = s.toCharArray();int[] count = new int[30];Arrays.fill(count, 0);Arrays.sort(c);for (int i = 0; i < c.length; i++) {int num = c[i] - 'A';count[num]++;}int max = 0;for (int i = 0; i < c.length; i++) {if(count[i]>max) {max = count[i];}}for (int i = 0; i < c.length; i++) {if(count[i] == max) {System.out.print((char)(i+'A'));}}}}
D 最少刷题数
package THL_0412;import java.util.Arrays;
import java.util.Scanner;public class D_2022 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int[] nums = new int[n];int[] count = new int[n];for (int i = 0; i < n; i++) {nums[i] = sc.nextInt();count[i] = nums[i];}Arrays.sort(count);int index = 0;if(n%2==0) {index = n/2+1;// 中间数,对于奇数长度,正好是中间的数;偶数长度,则是右边那个数}else {index = n/2;}int middle = count[index];for (int i = 0; i < n; i++) {if(nums[i]<middle) {System.out.print(middle - nums[i]+1);System.out.print(" ");}else {System.out.print(0);System.out.print(" ");}}}}
E 求阶乘
package THL_0412;import java.util.Scanner;public class E_2022 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);long k = sc.nextLong();long count; // 记录含有5的个数,也是阶乘后末尾0的个数long number = 5; // 从5的阶乘(120)开始判断while (true) {long temp = number;count = 0;while (temp > 0) {temp /= 5;count += temp;}if (count < k) {number += 5;} else if (count == k) {System.out.println(number);break;} else {System.out.println(-1); // 如果不存在第k个含有5的数,则输出-1break;}}}
}
(2021年能做出来45分,再加上骗分,应该有五十多)
这篇关于2024 蓝桥打卡Day40的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!