第十一届蓝桥杯Java B组真题

A-门牌制作

package 第十一届省赛;
 
public class _1门牌制作 {
	public static void main(String[] args) {
		int sum = 0;
		for(int i=1; i<=2020; i++) {
			int a = i/1000;
			int b = i/100%10;
			int c = i/10%10;
			int d = i%10;
			if(a==2)sum++;
			if(b==2)sum++;
			if(c==2)sum++;
			if(d==2)sum++;
		}
		System.out.println(sum);
	}
}

 

 

B-寻找2020

import java.util.Scanner;

public class Main {

	public static long count;

	public static void main(String[] args) {
		Scanner read = new Scanner(System.in); // Scanner read=new Scanner(new File("2020.txt"));
		String[] str = new String[300];
		for (int i = 0; i < 6; i++) {
			str[i] = read.nextLine();
		}
		read.close();
		f(str);
		System.out.println(count);
		read.close();
	}

	public static void f(String[] str) {
		for (int i = 0; i < str.length; i++) {
			for (int j = 0; j < str[i].length(); i++) {
				if (str[i].charAt(j) == '2') {
					// 横向
					if (str[i].length() - j >= 4 && str[i].charAt(j + 1) == '0' && str[i].charAt(j + 2) == '2'
							&& str[i].charAt(j + 3) == '0')
						count++;
					// 竖向
					if (str.length - i >= 4 && str[i + 1].charAt(j) == '0' && str[i + 2].charAt(j) == '2'
							&& str[i].charAt(j) == '0')
						count++;
					// 斜向
					if (str[i].length() - j >= 4 && str[i].length() - i >= 4 && str[i + 1].charAt(j + 1) == '0'
							&& str[i + 2].charAt(j + 2) == '2' && str[i + 3].charAt(j + 3) == '0')
						count++;
				}
			}
		}
	}
}

 

 

C-蛇形填数

import java.util.Scanner;

public class Main {

	// 推导:第一行第一列所在斜行有一个数,第二行第二列数字所在斜行有三个数,第几行第几列所在斜行数字的个数应该是奇数个...
	public static void main(String[] args) {
		int count = 0;
		int[] arr = new int[20];
		for (int i = 0; count < 20; i++) {
			if (i % 2 == 1)
				arr[count++] = i; // arr数组里存的是第count斜行有多少个数
		}
		System.out.println(arr[19]); // 输出的是第20行20列的元素所在的斜行的数的个数
		System.out.println((38 * 39) / 2); // 计算前一斜行以及之前共有多少数
		System.out.println((742 + 741 + 39) / 2); // 742:20行20列元素所在的斜行最开始的数字;741+39:20行20列所在的斜行最后的数字;二者相加除2就是第20行20列的数字
	}
}

 

 

D-七段码

题目描述
小蓝要用七段码数码管来表示一种特殊的文字。
七段码上图给出了七段码数码管的一个图示,数码管中一共有 7 段可以发光的二极管,分别标记为 a, b,
c, d, e, f, g。小蓝要选择一部分二极管(至少要有一个)发光来表达字符。在设计字符的表达时,要求
所有发光的二极管是连成一片的。
例如:b 发光,其他二极管不发光可以用来表达一种字符。
例如:c 发光,其他二极管不发光可以用来表达一种字符。这种方案与上一行的方案可以用来表示不同
的字符,尽管看上去比较相似。
例如:a, b, c, d, e 发光,f, g 不发光可以用来表达一种字符。

思路:
暴力,最后减去三种特殊情况(暴力判断不了的情况)

如下图:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int sum = 0;
		// 1表示量,0表示灭
		for (int a = 0; a <= 1; a++) {
			for (int b = 0; b <= 1; b++) {
				for (int c = 0; c <= 1; c++) {
					for (int d = 0; d <= 1; d++) {
						for (int e = 0; e <= 1; e++) {
							for (int f = 0; f <= 1; f++) {
								for (int g = 0; g <= 1; g++) {
									int s = a + b + c + d + e + f + g;
									if (s == 1) { // 此情况为只有一个灯亮(共7种情况)
										sum++;
										// printf("%d:%d%d%d%d%d%d%d\n",sum,a,b,c,d,e,f,g);
									} else if (s >= 2) { // s大于等于2为大前提(2表示有至少两个灯亮)
										// 有至少两个灯亮的前提下,把不挨着的情况去掉
										if (a == 1 && b == 0 && f == 0)
											continue;
										else if (b == 1 && a == 0 && c == 0 && g == 0)
											continue;
										else if (c == 1 && b == 0 && g == 0 && d == 0)
											continue;
										else if (d == 1 && e == 0 && c == 0)
											continue;
										else if (e == 1 && d == 0 && g == 0 && f == 0)
											continue;
										else if (f == 1 && a == 0 && e == 0 && g == 0)
											continue;
										sum++;
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(sum - 3); // 减去的三种情况为四个灯亮时,两个灯在一起两个灯在一起的情况,因为当两个灯亮,三个灯亮,五个灯亮六个灯亮,七个灯亮时不挨着的情况在上面的s>=2时已经去掉了
	}
}

 

 

E-排序

/*思路,逆序字符串,计算一下即可
           11个字母
           nmlkjihgfedcba (13+0)*14/2 = 13*7 = 91
           onmlkjihgfedcba 14*15/2 = 7*15 = 105
           jonmlkihgfedcba 100次交换
         */

 

代码段为验证输入字母串后需要多少次交换才可以换成正序的字母串
import java.util.Scanner;

public class Main {

	/*
	 * 思考后感觉完全逆序的字符串更有可能,所以倒序输入字母找到需要交换的次数: nmlkjihgfedcba需要交换91次,
	 * onmlkjihgfedcba需要交换105次,所以应该介于二者之间,有注意到105次的串如果把字母j放到首位可以减少5次达到100次. ---
	 * ---
	 * nmlkjihgfedcba (13+0)*14/2 = 13*7 = 91 
	 * onmlkjihgfedcba 14*15/2 = 7*15 = 105
	 * jonmlkihgfedcba 100次交换
	 * 
	 */
	public static void main(String[] args) {
		int count = 0;
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		int[] arr = new int[str.length()];
		for (int i = 0; i < str.length(); i++) {
			arr[i] = str.charAt(i);
		}

		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[i] > arr[j]) {
					count++;
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}

		System.out.println(count);
	}
}

 

 

F-成绩分析

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner writer = new Scanner(System.in);
		int n = writer.nextInt();
		int score = writer.nextInt(); // 第一个成绩输入
		int min = score, max = score;
		double sum = score;

		// 输入剩下的n-1个成绩
		for (int i = 2; i <= n; i++) { // 或者for (int i = 1; i < n; i++) {
			score = writer.nextInt();
			sum += score;
			if (min > score)
				min = score;
			if (max < score)
				max = score;
		}
		System.out.println(max);
		System.out.println(min);
		System.out.printf("%.2f", sum / n);
	}
}

 

 

G-单词分析

import java.util.Scanner;

/*
 * 在最后输出个数最多的字母时,错了很多次,也尝试了很多方法
 * */
public class Main {

	public static void main(String[] args) {
		Scanner writer = new Scanner(System.in);
		String str = writer.next();

		int[] arr = new int[26];
		for (int i = 0; i < str.length(); i++) {
			arr[str.charAt(i) - 'a']++;
		}
		int max = 0;
		for (int i = 1; i < 26; i++) {
			if (arr[i] > arr[max])
				max = i;
		}
		System.out.println((char) ((char) max + 'a')); // 想了很久,试了很多方法才用此方法输出个数多的字母
		System.out.println(arr[max]); // 对
	}
}

 

 

H-数字三角形(结合ALGO-4与ALGO-5一起看,原理差不多)

分析

DP推导+奇偶判断。
在输入数组的时候进行数组值的计算,因为只能向左或者右走,即我现在所在的位置坐标是从上一层这个位置的左边或者上边进行跳转得到的坐标,通过选择最大值进行跳转,更新数组的值
由于向左向右不能超过1,所以通过奇偶判断层数,
如果是奇数,最后的位置第n层,第(n/2+1)位置上的数字
如果是偶数,则需要判断第n层中第(n/2)位置的数字和第n层中第(n/2+1)位置的数字,选大的。
注意我的数组下标是从1开始。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); // 输入整数n表示三角形的行数
		int[][] arr = new int[n + 1][n + 1];
		for (int i = 1; i <= n; i++) { // i控制三角形有n行
			for (int j = 1; j <= i; j++) { // 三角形第一行有一个数,第二个有两个数,第三行有三个数...;j控制三角形中每行有几个数
				arr[i][j] = sc.nextInt();
				arr[i][j] += Math.max(arr[i - 1][j - 1], arr[i - 1][j]);
			}
		}
		/*
		 * 所以通过奇偶判断层数:如果是奇数,最后的位置第n层,第(n/2+1)位置上的数字;
		 * 如果是偶数,则需要判断第n层中第(n/2)位置的数字和第n层中第(n/2+1)位置的数字,选大的.
		 */
		System.out.println(n % 2 == 1 ? arr[n][n / 2 + 1] : Math.max(arr[n][n / 2], arr[n][n / 2 + 1]));
	}
}

 

 

I-子串分值和

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int sum = 0;
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		for (int i = 0; i < str.length(); i++) { // i控制首字母从0位置开始,从1位置开始...
			int[] arr = new int[str.length()]; // arr数组用来存str字符串中的字符
			int m = 0; //
			for (int j = i; j < str.length(); j++) { // j从i变化到str.length(),一个一个把从i变化到str.length()下标对应的字符存入arr数组
				arr[m++] = str.charAt(j); // 假设输入的str为"ababc":第一次arr存入a,执行a中f值为1;第二次arr多存入一个b变为了ab,ab的f值为2....每次存的直接拼接到上一次的arr数组后---(优点)
				int[] temp = new int[26]; // temp数组用来计算arr数组中的字符出现的个数,temp数组中0号下标对应值为arr数组中的字符a的个数(但其实只要temp中0下标号对应的元素值不为0就证明存在a)
				for (int k = 0; k < arr.length && arr[k] != 0; k++) {
					temp[arr[k] - 'a']++; // arr[k]-'a'为arr[k]元素在temp数组中存的下标
				}
				for (int n = 0; n < temp.length; n++) { // 每一次都把temp数组遍历一次,元素值不为0则sum++
					if (temp[n] != 0)
						sum++;
				}
			}
		}
		System.out.println(sum);
	}
}

 

 

J-装饰珠


时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分

问题描述

在怪物猎人这一款游戏中,玩家可以通过给装备镶嵌不同的装饰珠来获取
相应的技能,以提升自己的战斗能力。
已知猎人身上一共有 6 件装备,每件装备可能有若干个装饰孔,每个装饰
孔有各自的等级,可以镶嵌一颗小于等于自身等级的装饰珠 (也可以选择不镶
嵌)。
装饰珠有 M 种,编号 1 至 M,分别对应 M 种技能,第 i 种装饰珠的等级
为 Li ,只能镶嵌在等级大于等于 L i 的装饰孔中。
对第 i 种技能来说,当装备相应技能的装饰珠数量达到 K i个时,会产生W i ( K i ) 的价值。镶嵌同类技能的数量越多,产生的价值越大,即 W i ( K i − 1 ) < W i ( K i ) 。但每个技能都有上限 P i ( 1 ≤ P i ≤ 7 ) ,当装备的珠子数量超过 P i时,只会产生 W i ( P i ) 的价值。
对于给定的装备和装饰珠数据,求解如何镶嵌装饰珠,使得 6 件装备能得
到的总价值达到最大。

输入格式

输入的第 1 至 6 行,包含 6 件装备的描述。其中第 i 的第一个整数 N i 表示
第 i 件装备的装饰孔数量。后面紧接着 N i 个整数,分别表示该装备上每个装饰
孔的等级 L ( 1 ≤ L ≤ 4 )。
第 7 行包含一个正整数 M ,表示装饰珠 (技能) 种类数量。
第 8 至 M + 7 行,每行描述一种装饰珠 (技能) 的情况。每行的前两个整数
L j ( 1 ≤ L j ≤ 4 )和 P j ( 1 ≤ P i ≤ 7 )分别表示第 j 种装饰珠的等级和上限。接下来
P j个整数,其中第 k 个数表示装备该中装饰珠数量为 k 时的价值 W j ( k ) 。

输出格式

输出一行包含一个整数,表示能够得到的最大价值。

Input:

1 1
2 1 2
1 1
2 2 2
1 1
1 3
3
1 5 1 2 3 5 8
2 4 2 4 8 15
3 2 5 10
1
2
3
4
5
6
7
8
9
10

Output:

20
1

Explanation:

按照如下方式镶嵌珠子得到最大价值 18,括号内表示镶嵌的装饰珠的种类编号:
1: (1)
2: (1) (2)
3: (1)
4: (2) (2)
5: (1)
6: (2)
4 颗技能 1 装饰珠,4 颗技能 2 装饰珠 W1(4) + W2(4) = 5 + 15 = 20。
1
2
3
4
5
6
7
8
评测用例规模与约定

对于 30% 的评测用例,1 ≤ Ni ≤ 10, 1 ≤ M ≤ 20, 1 ≤ Wj(k) ≤ 500;
对于所有评测用例,1 ≤ Ni ≤ 50, 1 ≤ M ≤ 10000, 1 ≤ Wj(k) ≤ 10000。
 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值