ฅ(๑˙o˙๑)ฅ 大家好, 欢迎大家光临我的博客:面向阿尼亚学习
算法学习笔记系列持续更新中~
过两天就要考蓝桥杯了,今年报的java组,下午做了套第十二届蓝桥杯java组真题
分享一下
5道填空,5道编程
A、B、C、D、E为填空题
%%%
public class Main {
public static void main(String[] args)
{
System.out.println((int)'L');
}
}
答案:76
模拟
注意审题
可以从1拼到多少,结果记得减一
public class Main {
static int a[] = new int[10];
public static void main(String[] args) {
for (int i = 0; i <= 9; i++) {
a[i] = 2021;
}
for (int i = 1; i <= 10000; i++) {
int x = i;
while (x != 0) {
if (a[x % 10] == 0) {//如果卡片不够了,输出并返回
System.out.println(i - 1);// 注意是可以从1拼到多少
return;
}
a[x % 10]--;
x = x / 10;
}
}
}
}
答案:3181
枚举所有的点构成的直线,计算出斜率和截距,用来唯一表示一条直线
技巧用字符串类型Set,但要注意化简分数
import java.util.HashSet;
import java.util.Set;
public class Main {
static int gcd(int a, int b) {
if (b == 0)
return a;
else {
return gcd(b, a % b);
}
}
public static void main(String[] args) {
// 用字符串类型,方便去重
Set<String> lines = new HashSet<>();
// 循环所有的点
for (int x1 = 0; x1 < 20; x1++) {
for (int x2 = 0; x2 < 20; x2++) {
for (int y1 = 0; y1 < 21; y1++) {
for (int y2 = 0; y2 < 21; y2++) {
if (x1 == x2 || y1 == y2)
continue;
String line = "orz";// 分隔符没啥意义
// 计算斜率
int y = y2 - y1;
int x = x2 - x1;
int d = gcd(x, y);// 出最小公倍数化简
String k = y / d + "/" + x / d;
line += k + "orz";
// 计算截距
int m = y2 * x1 - y1 * x2;
int n = gcd(m, x);
line += m / n + "/" + x / n;
lines.add(line);
}
}
}
}
// 最后答案要加上k = 0及b = 0的情况
System.out.println(lines.size() + 20 + 21);
}
}
答案:40257
很明显枚举可以做,但是数量级有点大
技巧:我们只要枚举其所有因子即可
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Long num = new Long("2021041820210418");
// 从因子集合里面找结果
List<Long> set = new ArrayList<>();
for (long i = 1; i * i <= num; i++) {
if (num % i == 0) {
set.add(i);
if (num / i != i) {// 不相等加上
set.add(num / i);
}
}
}
int res = 0;
for (long i : set) {
for (long j : set) {
for (long t : set) {
if (i * j * t == num) {
res++;
}
}
}
}
System.out.println(res);
}
}
答案:2430
动态规划
毕竟最短路的本质就是动态规划
public class Main {
static final int N = 2022;
static int[] f = new int[N];// f[i]表示1到n的最短距离
static int gcd(int a, int b) {
if (b == 0)
return a;
else {
return gcd(b, a % b);
}
}
public static void main(String[] args) {
for (int i = 1; i <= 2021; i++) {
for (int j = i + 1; j <= i + 21 && j <= 2021; j++) {
if (f[j] == 0)
f[j] = f[i] + i * j / gcd(i, j);// i*j/gcd(i*j)为最小公倍数
else
f[j] = Math.min(f[j], f[i] + i * j / gcd(i, j));
}
}
System.out.println(f[2021]);
}
}
答案:10266837
以下为编程题
%%%如题所示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
// 先把毫秒转化成秒
n = n / 1000;
// 求最后一天的秒数
n = n % (24 * 60 * 60);
// 求显示的小时
long hour = n / (60 * 60);
// 这个小时内的秒数
n = n % (60 * 60);
// 求分钟
long min = n / 60;
// 求秒
long s = n % 60;
System.out.printf("%02d:%02d:%02d", hour, min, s);
}
}
1
的正整数数量4
的正整数数量13
的正整数数量3
+ 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
for (int i = 1;; i++) {
res = res * 3 + 1;
if (res >= n) {
System.out.println(i);
return;
}
}
}
}
根据杨辉三角形找规律,二分
import java.util.Scanner;
public class Main {
static int n;
static long C(int a, int b) {
long res = 1;
for (int i = a, j = 1; j <= b; i--, j++) {
res = res * i / j;
if (res > n)
return res;// 不用大于n
}
return res;
}
static boolean check(int k) {
long l = k * 2, r = Math.max((long) n, l);
while (l < r) {
long mid = l + r >> 1;
if (C((int) mid, k) >= n)
r = mid;
else
l = mid + 1;
}
if (C((int) r, k) != n)
return false;
System.out.println(r * (r + 1) / 2 + k + 1);
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int k = 16;; k--) {
if (check(k))
break;
}
}
}
暴力骗分
能过60%
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static final int N = 100007;
static Integer[] a = new Integer[N];
static int n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = i;
}
while (m-- > 0) {
int p = sc.nextInt();
int q = sc.nextInt();
if (p == 0)
Arrays.sort(a, 1, q + 1, (o1, o2) -> o2 - o1);//降序
else {
Arrays.sort(a, q, n + 1);//升序
}
}
for (int i = 1; i <= n; i++) {
System.out.print(a[i] + " ");
}
}
}
动态规划
import java.util.Scanner;
public class Main {
static int N = 5010;
static int MOD = (int) 1e9 + 7;
static int n;
static char[] str = new char[N];
static long[][] f;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
sc.close();
str = (" " + s).toCharArray();
n = s.length();
long l = work();
str = (" " + reverse(s)).toCharArray();
for (int i = 1; i <= n; i++) {
if (str[i] == '(') {
str[i] = ')';
} else {
str[i] = '(';
}
}
long r = work();
System.out.println(l * r % MOD);
}
private static long work() {
f = new long[N][N];
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
if (str[i] == '(') {
for (int j = 1; j <= n; j++) {
f[i][j] = f[i - 1][j - 1];
}
} else {
f[i][0] = (f[i - 1][0] + f[i - 1][1]) % MOD;
for (int j = 1; j <= n; j++) {
f[i][j] = (f[i - 1][j + 1] + f[i][j - 1]) % MOD;
}
}
}
for (int i = 0; i <= n; i++) {
if (f[n][i] > 0) {
return f[n][i];
}
}
return -1;
}
private static String reverse(String s) {
return new StringBuffer(s).reverse().toString();
}
}
祝考的都会