目录
[A 报数游戏](#A 报数游戏)
[B 类斐波那契循环数](#B 类斐波那契循环数)
[C 分布式队列](#C 分布式队列)
[D 食堂](#D 食堂)
[E 最优分组](#E 最优分组)
[F 星际旅行](#F 星际旅行)
[G LITS 游戏](#G LITS 游戏)
[H 拼十字](#H 拼十字)
今天心血来潮把去年的题目又做了一遍...
本人去年大一 拿的是全省第五进的国赛
而如今的已经是一名 codeforces 1500 分的入门级别的算竞选手了
下周又是蓝桥杯了 想起来后又做了一遍
仅代表个人做题记录

A 报数游戏
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("2429042904288");
//在此输入您的代码...
scan.close();
}
}
B 类斐波那契循环数
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("7913837");
//在此输入您的代码...
scan.close();
}
}
C 分布式队列
模拟题
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static final long MOD = (long) (1e9+7);
public static void solve() throws IOException {
int n=sc.nextInt();
// 主节点的数字
int ans1=0;
// 其余结点
int arr[]=new int[n-1];
while(sc.hasNext()) {
String str=sc.next();
if(str.equals("add")) {
// 添加
int k = sc.nextInt();
ans1++;
}else if(str.equals("sync")){
// 同步
int k = sc.nextInt()-1;
if(arr[k]<ans1) {
arr[k]++;
}
}else if(str.equals("query")) {
// 查询
int ans=Integer.MAX_VALUE;
for(int j=0;j<n-1;j++) {
ans=Math.min(ans, arr[j]);
}
System.out.println(ans);
}
}
}
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {solve();}
}
}
D 食堂
数据范围很小啊
把每一种情况都列举出来了
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
// 食堂
// xixi♡西
public class Main {
static Scanner sc = new Scanner(System.in);
static final int mod = (int) (1e9 + 7);
/**
* @throws IOException
*/
private static void solve() throws IOException {
// todo
int a2=sc.nextInt();
int a3=sc.nextInt();
int a4=sc.nextInt();
int b4=sc.nextInt();
int b6=sc.nextInt();
int ans = 0;
while (b6 > 0) { // 3+3
if (a3 >= 2) {
b6--;
a3 -= 2;
ans += 6;
} else {
break;
}
}
while (b6 > 0) { // 4+2
if (a4 > 0 && a2 > 0) {
b6--;
a4--;
a2--;
ans += 6;
} else {
break;
}
}
while (b6 > 0) { // 2+2+2
if (a2 >= 3) {
b6--;
a2 -= 3;
ans += 6;
} else {
break;
}
}
while (b6 > 0) { // 3+2
if (a3 > 0 && a2 > 0) {
b6--;
a3--;
a2--;
ans += 5;
} else {
break;
}
}
while (b6 > 0) { // 4
if (a4 > 0) {
b6--;
a4--;
ans += 4;
} else {
break;
}
}
if (b6 > 0 && a2 == 2) { // 2+2
ans += 4;
b6--;
a2 = 0;
}
if (b6 > 0 && a3 > 0) { // 3
ans += 3;
b6--;
a3 = 0;
}
if (b6 > 0 && a2 > 0) { // 2
ans += 2;
a2 = 0;
}
while (b4 > 0) { // 4
if (a4 > 0) {
b4--;
a4--;
ans += 4;
} else {
break;
}
}
while (b4 > 0) { // 2+2
if (a2 >= 2) {
b4--;
a2 -= 2;
ans += 4;
} else {
break;
}
}
while (b4 > 0) { // 3
if (a3 > 0) {
b4--;
a3--;
ans += 3;
} else {
break;
}
}
if (b4 > 0 && a2 > 0) { // 2
ans += 2;
}
System.out.println(ans);
}
public static void main(String[] args) throws Exception {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
}
E 最优分组
注意用小数进行运算即可
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static final long MOD = (long) (1e9+7);
public static void solve() throws IOException {
long n=sc.nextLong(); // N只宠物
double p=1.0-sc.nextDouble(); // 没被感染的概率
if(p==1){
System.out.println("0");
return;
}
if(p==0){
System.out.println(n);
return;
}
if(n==1){
System.out.println("1");
return;
}
double num=n+1; // 消耗的数目
long k=0;
for(int i=1;i<=n;i++) { // i个人一组 每组恰好有i只宠物
double ans = 0; // 消耗的试剂
double ans1 = 0; // 分成多少组
if(n%i==0) {
ans1=n/i*1.0;
}else {
continue;
}
// 先统一测试
ans+=ans1;
// 恰好分成ans1组 每组i个人
// 全部是阴性的概率
double quanbuyingxing=1.0;
for (int i1 = 0; i1 < i; i1++) {
quanbuyingxing*=p;
}
// 有阳性的概率
double youyang=1.0-quanbuyingxing;
// 有多少组阳性的
double yangzu=youyang*ans1*1.0;
// 分别测
ans+=yangzu*i;
if(ans<num){
num=ans;
k=i;
}
}
System.out.println(k);
}
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {solve();}
}
}
F 星际旅行
我自己搓的版本 bfs
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static ArrayList<ArrayList<Integer>>adj;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 邻接矩阵
adj=new ArrayList<>();
int n=sc.nextInt(); // n个星球
int m=sc.nextInt(); // m道传送门
int q=sc.nextInt(); // 盲盒
long sum=0;
for (int i = 0; i < n+5; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u=sc.nextInt();
int v=sc.nextInt();
// 双向图
adj.get(u).add(v);
adj.get(v).add(u);
}
int arr[][]=new int[q][2];
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < q; i++) {
int u=sc.nextInt(); // 当前节点
int cnt=sc.nextInt(); // 旅行卷轴 多少层
arr[i][0]=u;
arr[i][1]=cnt;
if(hm.containsKey(u)==false){
hm.put(u,cnt);
}else {
int value= hm.get(u);
hm.put(u,Math.max(value,cnt));
}
}
ArrayList<ArrayList<Integer>>yuchuli=new ArrayList<>();
for (int i = 0; i < n+5; i++) {
yuchuli.add(new ArrayList<>());
}
for (Integer i : hm.keySet()) {
int u=i; // 当前节点
int max=hm.get(u); // 最大旅行卷轴
// System.out.println(u+" "+max);
int ans=1; // 当前层有1个
Queue<Integer>queue=new LinkedList<>();
queue.add(u);
int jishu=0; // 计数
int sumsum=0;
// System.out.println(sumsum);
while (!queue.isEmpty()){
Integer poll = queue.poll();
// System.out.println(u+" "+poll);
jishu++; // 计数
for(int in:adj.get(poll)){
if(in!=u){
queue.add(in);
}
}
if(jishu==ans){
ans=queue.size();
// System.out.println("当前层有"+ans);
sumsum+=jishu;
// System.out.println(u+" "+sumsum);
yuchuli.get(u).add(sumsum);
jishu=0;
max--;
if(max<0){
break;
}
}
}
}
for (int i = 0; i < q; i++) {
Integer i1 = yuchuli.get(arr[i][0]).get(arr[i][1]);
// System.out.println(i1);
sum+=i1;
}
System.out.println(String.format("%.2f",(double)sum/(double) n/1.0));
}
}
这是看了题解的版本 Floyd算法
import java.util.Scanner;
public class Main {
static int[][] d = new int[1010][1010];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n, m, Q;
n = sc.nextInt();
m = sc.nextInt();
Q = sc.nextInt();
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (i == j){
d[i][j] = 0;
}
else {
d[i][j] = (int) 1e9;
}
}
}
for (int i = 0; i < m; i++) {
int a, b;
a = sc.nextInt();
b = sc.nextInt();
d[a][b] = d[b][a] = 1;
}
// Floyd算法
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
double ans = 0;
for (int i = 0; i < Q; i++) {
int x, y;
x = sc.nextInt();
y = sc.nextInt();
for (int j = 1; j <= n; j++){
if (d[x][j] <= y) {
ans = ans + 1;
}
}
}
System.out.printf("%.2f", ans / Q);
}
}
G LITS 游戏
不会
import java.io.*;
import java.math.*;
import java.util.*;
// LITS 游戏
public class Main {
static Scanner sc = new Scanner(System.in);
static final long MOD = (long) (1e9+7);
public static void solve() throws IOException {
System.out.println("NO");
}
public static void main(String[] args) throws Exception {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {solve();}
}
}
H 拼十字
直接暴力的
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static final long MOD = (long) (1e9+7);
static class Pair{
// 长
long l;
// 宽
long w;
// 带参构造
Pair(long ll,long ww){
this.l=ll;
this.w=ww;
}
}
public static void solve() throws IOException {
int n=sc.nextInt();
ArrayList<Pair>list0=new ArrayList<>();
ArrayList<Pair>list1=new ArrayList<>();
ArrayList<Pair>list2=new ArrayList<>();
for(int i=0;i<n;i++) {
long l=sc.nextLong(); // 长
long w=sc.nextLong(); // 宽
long c=sc.nextLong(); // 颜色
if(c==0) {
list0.add(new Pair(l,w));
}else if(c==1){
list1.add(new Pair(l,w));
}else {
list2.add(new Pair(l,w));
}
}
long cnt=0;
// 颜色0 和 颜色 1
for(int i=0;i<list0.size();i++) {
for(int j=0;j<list1.size();j++) {
long chang1 = list0.get(i).l;
long kuan1 = list0.get(i).w;
long chang2 = list1.get(j).l;
long kuan2 = list1.get(j).w;
if( (chang1>chang2&&kuan1<kuan2) || (chang1<chang2&&kuan1>kuan2)) {
cnt++;
}
}
}
// 颜色1 和 颜色2
for(int i=0;i<list1.size();i++) {
for(int j=0;j<list2.size();j++) {
long chang1 = list1.get(i).l;
long kuan1 = list1.get(i).w;
long chang2 = list2.get(j).l;
long kuan2 = list2.get(j).w;
if( (chang1>chang2&&kuan1<kuan2) || (chang1<chang2&&kuan1>kuan2)) {
cnt++;
}
}
}
// 颜色0 和 颜色2
for(int i=0;i<list0.size();i++) {
for(int j=0;j<list2.size();j++) {
long chang1 = list0.get(i).l;
long kuan1 = list0.get(i).w;
long chang2 = list2.get(j).l;
long kuan2 = list2.get(j).w;
if( (chang1>chang2&&kuan1<kuan2) || (chang1<chang2&&kuan1>kuan2)) {
cnt++;
}
}
}
System.out.print(cnt);
}
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {solve();}
}
}