十二届蓝桥杯模拟赛

第一题


  请问在 1 到 2020 中,有多少个数与 2020 互质,即有多少个数与 2020 的最大公约数为 1。
题目解析
会gcd就是送分题

public class test1 {
	public static void main(String[] args) {
		int count=0;
		for (int i = 1; i < 2020; i++) {
			if (gcd(i, 2020)==1) {
				count++;
				
			}
		}
		System.out.println(count);
	}
	public static int gcd(int a,int b) {
		return b==0?a:gcd(b, a%b);
	}
}

第二题


  ASCII 码将每个字符对应到一个数值(编码),用于信息的表示和传输。在 ASCII 码中,英文字母是按从小到大的顺序依次编码的,例如:字母 A 编码是 65, 字母 B 编码是 66,字母 C 编码是 67,请问字母 Q 编码是多少?
题目解析
这题会数数就行,代码都不用写

public class test2 {
	public static void main(String[] args) {
		char a='Q';
		int b=a;
		System.out.println(b);
	}	
}

第三题


对于整数 v 和 p,定义 Pierce 序列为:
  a[1] = v
  a[i] = p % a[i-1]
  例如,当 v = 8, p = 21 时,对应的 Pierce 序列为
  a[1] = 8
  a[2] = 5
  a[3] = 1
  当 p=2021 时,最长的 Pierce 序列出现在 v=1160 时,请问这个序列有多长?
题目解析

public class test3 {
	public static void main(String[] args) {
		int v=1160;
		int p=2021;
		int count=0;
		while (v!=0) {
			count++;
			System.out.print(v+" ");
			v=p%v;			
		}
		System.out.println("长度:"+count);
	}
}

第四题


有一棵二叉树,一个由2021个结点,其中有1000个结点有两个子结点,其他的结点有一个或者0个子结点。
  请问,这棵二叉树有多少个叶结点?
题目解析
n0=n2+1
根据公式,答案:1001

第五题


在 Excel 中,第 1 列到第 26 列的列名依次为 A 到 Z,从第 27 列开始,列名有两个字母组成,第 27 列到第 702 列的列名依次为 AA 到 ZZ。
  之后的列再用 3 个字母、4 个字母表示。
  请问,第 2021 列的列名是什么?
题目解析
这题计算26进制,如果闲写代码麻烦,打开excel看看列名就行

public class test5 {
	public static void main(String[] args) {
		char A;		
		char B;
		char C;
		for (int i = 0; i < 26; i++) {
			for (int j = 0; j < 26; j++) {
				for (int j2 = 0; j2 < 26; j2++) {
					if (j2+j*26+i*26*26==2021) {						
						A=(char) (i+64);
						B=(char) (j+64);
						C=(char) (j2+64);
						System.out.println(A+""+B+""+C);						
					}							
				}
			}
		}			
	}
}

第六题


斐波那契数列是这样一个数列:它的第一项和第二项都是1,从第三项开始每一项都是前两项的和。
  根据以上定义,我们容易计算出斐波那契数列的前几项依次是:1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ……
  现在请你计算斐波那契数列第N项是奇数还是偶数?
题目解析
其实规律是奇奇偶,当时居然没看出来

import java.util.Scanner;
public class test6 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n = sc.nextInt();
		long a=1;
		long b=1, temp =0;
		for (long i = 2; i < n; i++) {
			temp=a+b;
			a=b;
			b=temp;			
		}
		long x=b%2;
		System.out.println(x);
	}	
}

第七题


  小蓝正在写一个网页显示一个新闻列表,他需要将总共 n 条新闻显示,每页最多可以显示 p 条,请问小蓝至少需要分多少页显示?
  例如,如果要显示2021条新闻,每页最多显示10条,则至少要分203页显示。
输入格式
  输入的第一行包含一个整数 n,表示要显示的新闻条数。
  第二行包含一个整数 p,表示每页最多可以显示的条数。
输出格式
  输出一个整数,表示答案。
样例输入
2021
10
样例输出
203
样例输入
2020
20
样例输出
101
数据规模和约定
对于所有评测用例,1 <= n <= 10000,1 <= p <= 100。
题目解析
都第七题了还在送分

public class test8 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n = sc.nextInt();
		int p = sc.nextInt();
		int x=n%p;
		int y=n/p;
		if (x!=0) {
			System.out.println(y+1);
		}else {
			System.out.println(y);						
		}
	}
}

第八题


在书写一个较大的整数时,为了方便看清数位,通常会在数位之间加上逗号来分割数位,具体的,从右向左,每三位分成一段,相邻的段之间加一个逗号。
  例如,1234567 写成 1,234,567。
  例如,17179869184 写成 17,179,869,184。
  给定一个整数,请将这个整数增加分割符后输出。
输入格式
  输入一行包含一个整数 v。
输出格式
  输出增加分割符后的整数。
样例输入
1234567
样例输出
1,234,567
题目解析
处理成字符串数组,按规则输出

public class test7 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String n = sc.next();		
		String[] s = n.split("");		
		if (s.length%3==0) {
			for (int i = 0; i < s.length; i++) {
				if (i%3==0&&i!=0) {				
					System.out.print(",");
				}
				System.out.print(s[i]);
			}
		}else if (s.length%3==1) {
			System.out.print(s[0]+",");
			for (int i = 1; i < s.length; i++) {
				if (i%3==1&&i!=1) {					
					System.out.print(",");
				}
				System.out.print(s[i]);
			}
		}else{
			System.out.print(s[0]+s[1]+",");
			for (int i = 2; i < s.length; i++) {
				if (i%3==2&&i!=2) {					
					System.out.print(",");
				}
				System.out.print(s[i]);
			}
		}								
	}	
}

第九题


  给定一张图片,由 n 行 m 列像素组成,每个像素由一个范围在 0 到 255 的整数表示。
  例如,下面是一张 4 行 7 列的图片。
  8 232 229 23 21 10 247
  25 252 238 17 241 9 245
  1 243 251 32 236 31 253
  13 5 255 8 13 24 11
  对于每个像素,请找出以这个像素为中心的3行3列中最亮(数值最大)的像素值。
  例如,第 2 行第 2 列像素值为 252,而它周围 8 个像素都没有它亮,因此第 2 行第 2 列对应的值为 252。
  第 3 行第 2 列对应的值为255。
  第 1 行第 1 列为中心不足 3 行 3 列,最大值为 252。
  将每个像素对应的值写成上面图片的样子,得到:
  252 252 252 241 241 247 247
  252 252 252 251 241 253 253
  252 255 255 255 241 253 253
  243 255 255 255 236 253 253
输入格式
  输入第一行包含两个整数 n, m,分别表示图片的行数和列数。
  接下来 n 行,每行 m 个整数,表示一个像素。
输出格式
  输出 n 行,每行 m 个整数,表示以每个像素为中心的3行3列中最亮的像素值。
样例输入
4 7
8 232 229 23 21 10 247
25 252 238 17 241 9 245
1 243 251 32 236 31 253
13 5 255 8 13 24 11
样例输出
252 252 252 241 241 247 247
252 252 252 251 241 253 253
252 255 255 255 241 253 253
243 255 255 255 236 253 253
数据规模和约定
  对于所有评测用例,图片的行数和列数均不超过 100,每个像素的值为 0 到 255 之间的整数。
题目解析
这题直接暴力,最大的矩阵也只要几万算力,不会超时

public class test10 {
	static int n;
	static int m;
	static int arr[][];
	static int arrcy[][];
	//x,y数组移动方向
	static int x[]= {0,0,1,-1,1,1,-1,-1};
	static int y[]= {1,-1,0,0,1,-1,1,-1};
	
	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		//输入数组
		arr=new int[n][m];
		//输出数组
		arrcy=new int[n][m];
		//初始化
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int m=sc.nextInt();
				arrcy[i][j]=m;
				arr[i][j]=m;
			}
		}
		find();
		//输出
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				System.out.print(arrcy[i][j]+" ");
			}
			System.out.println();
		}
	}
	//依次遍历
	public static void find() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				put(i, j);
			}
		}
	}
	//寻找最大像素
	public static void put(int a,int b) {
		int max=arr[a][b];
		for (int i = 0; i < 8; i++) {
			if ((a+x[i])>=0&&(b+y[i])>=0&&(a+x[i])<n&&(b+y[i])<m) {
				if (max<arr[a+x[i]][b+y[i]]) {
					max=arr[a+x[i]][b+y[i]];
					arrcy[a][b]=max;
				}
			}
		}
	}
}

第十题


  杂货铺老板一共有N件物品,每件物品具有ABC三种属性中的一种或多种。从杂货铺老板处购得一件物品需要支付相应的代价。
  现在你需要计算出如何购买物品,可以使得ABC三种属性中的每一种都在至少一件购买的物品中出现,并且支付的总代价最小。
输入格式
  输入第一行包含一个整数N。
  以下N行,每行包含一个整数C和一个只包含"ABC"的字符串,代表购得该物品的代价和其具有的属性。
输出格式
  输出一个整数,代表最小的代价。如果无论如何凑不齐ABC三种属性,输出-1。
样例输入
5
10 A
9 BC
11 CA
4 A
5 B
样例输出
13
数据规模和约定
  对于50%的评测用例,1 <= N <= 20
  对于所有评测用例,1 <= N <= 1000, 1 <= C <= 100000
题目解析
这题直接暴力会超时,我们可以将所有字符串分为7类
含ABC 含AB 含AC 含BC 含A 含B 含C
将这七类的最小权值放入对应的数组
再在这七类组合,找含ABC的最小权值组合
因为要判断数组中是否有值所以判断比较麻烦,代码有些许臃肿

public class test9_2 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int N = sc.nextInt();
		Integer arr[]=new Integer[7];
		int max=-1;
		int temp1;
		String temp2;
		String []s= {"ABC","A","B","C","AB","BC","AC"};
		for (int i = 0; i < N; i++) {
			temp1=sc.nextInt();
			temp2=sc.next();
			//含有ABC的字符串
			if (temp2.indexOf('A')!=-1&&temp2.indexOf('B')!=-1&&temp2.indexOf('C')!=-1) {
				if (arr[0]==null||temp1<arr[0]) {
					arr[0]=temp1;
				}
			}
			//只含有A的字符串
			if (temp2.indexOf('A')!=-1&&temp2.indexOf('B')==-1&&temp2.indexOf('C')==-1) {
				if (arr[1]==null||temp1<arr[1]) {
					arr[1]=temp1;					
				}
			}
			//只含有B的字符串
			if (temp2.indexOf('A')==-1&&temp2.indexOf('B')!=-1&&temp2.indexOf('C')==-1) {
				if (arr[2]==null||temp1<arr[2]) {
					arr[2]=temp1;
				}
			}
			//只含有C的字符串
			if (temp2.indexOf('A')==-1&&temp2.indexOf('B')==-1&&temp2.indexOf('C')!=-1) {
				if (arr[3]==null||temp1<arr[3]) {
					arr[3]=temp1;
				}
			}
			//只含有AB的字符串
			if (temp2.indexOf('A')!=-1&&temp2.indexOf('B')!=-1&&temp2.indexOf('C')==-1) {
				if (arr[4]==null||temp1<arr[4]) {
					arr[4]=temp1;
				}
			}
			//只含有BC的字符串
			if (temp2.indexOf('A')==-1&&temp2.indexOf('B')!=-1&&temp2.indexOf('C')!=-1) {
				if (arr[5]==null||temp1<arr[5]) {
					arr[5]=temp1;
				}
			}
			//只含有AC的字符串
			if (temp2.indexOf('A')!=-1&&temp2.indexOf('B')==-1&&temp2.indexOf('C')!=-1) {
				if (arr[6]==null||temp1<arr[6]) {
					arr[6]=temp1;
				}
			}						
		}
		for (int i = 0; i < s.length; i++) {
			//寻找A
			if (s[i].indexOf('A')!=-1&&arr[i]!=null) {
				for (int j = 0; j < s.length; j++) {
					//寻找B
					if (s[j].indexOf('B')!=-1&&arr[j]!=null) {
						for (int j2 = 0; j2 < s.length; j2++) {
							//寻找C
							if (s[j2].indexOf('C')!=-1&&arr[j2]!=null) {
								if (i==j&&i!=j2) {
									if (max==-1) {
										max=arr[i]+arr[j2];
									}
									if (max>arr[i]+arr[j2]) {
										max=arr[i]+arr[j2];
									}
								}else if (j==j2&&j!=i) {
									if (max==-1) {
										max=arr[i]+arr[j2];
									}
									if (max>arr[i]+arr[j2]) {
										max=arr[i]+arr[j2];
									}
								}else if (i==j2&&j!=i) {
									if (max==-1) {
										max=arr[j]+arr[j2];
									}
									if (max>arr[j]+arr[j2]) {
										max=arr[j]+arr[j2];
									}
								}else if (i==j&&j==j2) {
									if (max==-1) {
										max=arr[j];
									}
									if (max>arr[j]) {
										max=arr[j];
									}
								}else {
									if (max==-1) {
										max=arr[j]+arr[i]+arr[j2];
									}
									if (max>arr[j]+arr[i]+arr[j2]) {
										max=arr[j]+arr[i]+arr[j2];
									}
								}
							}													
						}
					}													
				}
			}													
		}
		System.out.println(max);
	}	
}

或者不分类直接暴力,会略微超时(6s)

public class test9 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int N = sc.nextInt();
		int arr[]=new int[N];
		int max=-1;
		String s[]=new String[N];		
		for (int i = 0; i < N; i++) {
			arr[i]=sc.nextInt();
			s[i]=sc.next();
		}
		
		for (int i = 0; i < s.length; i++) {
			if (s[i].indexOf("A")!=-1) {
				for (int j = 0; j < s.length; j++) {
					if (s[j].indexOf("B")!=-1) {
						for (int j2 = 0; j2 < s.length; j2++) {
							if (s[j2].indexOf("C")!=-1) {								
								//1
								if (i==j&&i!=j2) {
									if (max==-1) {
										max=arr[i]+arr[j2];
									}
									if (max>arr[i]+arr[j2]) {
										max=arr[i]+arr[j2];
									}
								}else if (j==j2&&j!=i) {
									if (max==-1) {
										max=arr[i]+arr[j2];
									}
									if (max>arr[i]+arr[j2]) {
										max=arr[i]+arr[j2];
									}
								}else if (i==j2&&i!=j) {
									if (max==-1) {
										max=arr[j]+arr[j2];
									}
									if (max>arr[j]+arr[j2]) {
										max=arr[j]+arr[j2];
									}
								}else if (i==j&&j==j2) {
									if (max==-1) {
										max=arr[j];
									}
									if (max>arr[j]) {
										max=arr[j];
									}
								}else {
									if (max==-1) {
										max=arr[j]+arr[i]+arr[j2];
									}
									if (max>arr[j]+arr[i]+arr[j2]) {
										max=arr[j]+arr[i]+arr[j2];
									}
								}																
							}							
						}
					}
				}
			}
		}				
		System.out.println(max);
	}
}

完结撒花
评论区欢迎指正

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值