Java实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)
Posted 发呆哥o_o ....
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)相关的知识,希望对你有一定的参考价值。
如有错误,还请佬 评论或私信指出(写的稍些急)
等后面在补充补充解题思路,在补一套C++实现,再也不肝博客了
/(ㄒoㄒ)/~~(由于上传图片有限制,如果描述不清楚点击手写题解)
手写题解字丑,但是小编感觉会比文字看的直观一些(其实是不会用数学公式)
介意字丑勿看
手写题解(如果访问出错,请刷新访问的页面即可(Nebo的问题))
A:空间
B:卡片
C:直线
D:货物摆放
E:路径
F:时间显示
G:砝码称重
H:杨辉三角形
I:双向排序
J:括号序列
A:空间
package LanqiaobeiExam._12CB;
/**
* ClassName: A空间
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 15:01
* Author: asleep
*/
public class A空间 //1B = 8bit(位) 32位 = 4B 1MB = 1024KB = 1024 * 1024B
public static void main(String[] args) //67108864
System.out.println(256 * 1024 * 1024 / 4);
B:卡片
package LanqiaobeiExam._12CB;
/**
* ClassName: B卡片
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 15:19
* Author: asleep
*/
public class B卡片 //3181 从1开始循环数字,每个数字循环每一位,对应数字卡片 -1,减到 0 表示当前数字不能拼成
public static void main(String[] args)
int[] count = new int[10];
for (int i = 0; i < 10; i++)
count[i] = 2021;
int index = 0;
A:
while (true)
int temp = ++index;
while (temp != 0)
if (count[temp % 10] > 0)
count[temp % 10]--;
else
System.out.println(index - 1);
break A;
temp /= 10;
C:直线
package LanqiaobeiExam._12CB;
import java.util.*;
/**
* ClassName: C直线
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 15:29
* Author: asleep
*/
public class C直线 //40257
public static class Node
int x, y;
public Node(int x, int y)
this.x = x;
this.y = y;
double x1, y1;
public Node(double x1, double y1)
this.x1 = x1;
this.y1 = y1;
public static class Line
int a, b, c;
public Line(int a, int b, int c)
this.a = a;
this.b = b;
this.c = c;
@Override
public boolean equals(Object o)
Line line = (Line) o;
return a == line.a && b == line.b && c == line.c;
@Override
public int hashCode()
return Objects.hash(a, b, c);
public static int gcd(int a, int b)
return b == 0 ? a : gcd(b, a % b);
public static int gcd(int a, int b, int c)
return gcd(gcd(a, b), gcd(b, c));
public static void main(String[] args)
//方法一 两点式
int n = 20, m = 21;
Set<Line> set = new HashSet<>();
Node[] node = new Node[1000];
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
node[index++] = new Node(i, j);
for (int i = 0; i < index; i++)
for (int j = i + 1; j < index; j++)
int a = node[i].y - node[j].y;//系数
int b = node[j].x - node[i].x;
int c = node[i].y * (node[i].x - node[j].x) - node[i].x * (node[i].y - node[j].y);
int g = gcd(Math.abs(a), Math.abs(b), Math.abs(c));
set.add(new Line(a / g, b / g, c / g));
System.out.println(set.size());
//方法二: y = kx+b 确定直线
List<Integer> points = new ArrayList<>();
Set<String> ans = new HashSet<>();
for (int i = 0; i < 20; i++)
for (int j = 0; j < 21; j++)
points.add(i * 100 + j); //存xy
int size = points.size();
for (int i = 0; i < size; i++)
int node1 = points.get(i);
int x1 = node1 / 100, y1 = node1 % 100;
for (int j = i + 1; j < size; j++)
int node2 = points.get(j);
int x2 = node2 / 100, y2 = node2 % 100;
int up = y1 - y2, down = x1 - x2;
if (down == 0)
ans.add("x = " + x1);
continue;
int c1 = gcd(up, down);
String K = up / c1 + " " + down / c1;
int Up = y1 * down - x1 * up;
int c2 = gcd(Up, down);
String B = Up / c2 + " " + down / c2;
ans.add(K + " " + B);
System.out.println(ans.size());
//方法三:
//两点式两个点交换后为一条新的直线,需要除 2
Set<Line> set = new HashSet<>();
int n = 20, m = 21;
for (int x1 = 0; x1 < n; x1++)
for (int y1 = 0; y1 < m; y1++)
for (int x2 = 0; x2 < n; x2++)
for (int y2 = 0; y2 < m; y2++)
if (x1 == x2 && y1 == y2) //两个相同的点不能确定直线
continue;
int a =y1 - y2, b = x2 - x1;
int c = x1 * (y2 - y1) - y1 * (x2 - x1);
int g = gcd(Math.abs(a), Math.abs(b), Math.abs(c));
set.add(new Line(a / g, b / g, c / g));
System.out.println(set.size() / 2);
D:货物摆放
package LanqiaobeiExam._12CB;
/**
* ClassName: D货物摆放
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 17:00
* Author: asleep
*/
public class D货物摆放 //2430
public static void main(String[] args) //直接遍历 n 会炸,我们这里遍历 n 的约数,相对复杂度会低很多
long n = 2021041820210418L;
long[] res = new long[10000];
int index = 0;
for (long i = 1L; i <= n / i; i++)
if (n % i == 0)
res[index++] = i;
if (i * i != n)
res[index++] = n / i;
int count = 0;
for (int i = 0; i < index; i++)
for (int j = 0; j < index; j++)
if (n % (res[i] * res[j]) == 0)
count++;
System.out.println(count);
E:路径
package LanqiaobeiExam._12CB;
import java.util.Arrays;
/**
* ClassName: E路径
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 17:08
* Author: asleep
*/
public class E路径 //10266837
static int[][] map = new int[2022][2022];
public static int gcd(int a, int b)
return b == 0 ? a : gcd(b, a % b);
public static int dijkstra()
int[] distance = new int[2022];
Arrays.fill(distance, 0xffffff);
boolean[] visited = new boolean[2022];
distance[1] = 0;
for (int i = 1; i < 2022; i++)
int temp = -1;
for (int j = 1; j < 2022; j++)
if (!visited[j] && (temp == -1 || distance[j] < distance[temp]))
temp = j;
visited[temp] = true;
for (int j = 1; j < 2022; j++)
distance[j] = Math.min(distance[j], distance[temp] + map[temp][j]);
return distance[2021];
public static void main(String[] args)
for (int i = 1; i < 2022; i++)
for (int j = 1; j < 2022; j++)
if (i != j)
if (Math.abs(i - j) <= 21)
map[i][j] = i * j / gcd(i, j);
map[j][i] = map[i][j];
else
map[i][j] = 0xffffff;
map[j][i] = map[i][j];
System.out.println(dijkstra());
F:时间显示
package LanqiaobeiExam._12CB;
import java.util.Scanner;
/**
* ClassName: F时间显示
* Package: LanqiaobeiExam._12CB
*
* @DATE: 2022/3/21 20:18
* Author: asleep
*/
public class F时间显示 //无需考虑天数,只要一天内的时间,直接取余算 时分秒 即可
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
n /= 1000;
long second = n % 60;
n 以上是关于Java实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)的主要内容,如果未能解决你的问题,请参考以下文章
Java && C++ 实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)
Java && C++ 实现第十二届蓝桥杯 C++ B组 省赛真题(希望能和各位佬能一起讨论算法题:讨论群:99979568)
第十二届蓝桥杯 2021年省赛真题 (Java 大学B组) 第一场 (更新中)