一、前言
接着上一篇蓝桥杯Java组基础巩固,此篇进行蓝桥杯Java组近两年的真题解析(个人解题+相关整理),代码能力弱,还望大佬多多指正。
1.相关题解以及训练
2012-2021蓝桥杯历届真题
2020年十一届JavaB组简单思路分析
蓝桥杯训练系统
近年蓝桥杯题目表格形式汇总
2.DP 类难题
字符串排序问题
装饰珠
数字三角形
3.模拟类难题
4.思维类难题
5.前缀和
异能传输
6.DFS、BFS
七段码
二、个人整理
三、部分代码
说明该代码仅代表个人思路,可能还存在一些问题,后期(4.18之前)会进行矫正!
2020年B组
public class TestB {
// 1.门牌制作
// 解题思路:遍历1-2020的数累加每个数含有2的数量
// 发散“
// 遍历计数
// 结果:624
void test1() {
int sum = 0;
for(int i = 1;i<=2020;i++)
{
String temp = i+"";
for(int j=0;j<temp.length();j++) {
if(temp.charAt(j)=='2') {
sum+=1;
}
}
}
System.out.print("rs:"+sum);
}
// 寻找2020
// 解题思路:先将数据存入一个矩阵中,然后分别遍历行、斜、列进行计数
// 遍历计数+IO
void test2() {
int[][] maze = new int[100][100];
int row=0,col =0;
int sum=0;
try {
InputStream ips = new FileInputStream("E:\\eclipse-workspace\\java-lq\\files\\2020.txt");
InputStreamReader isr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(isr);
String temp = null;
while((temp = br.readLine())!=null) {
for(int i =0;i<temp.length();i++) {
maze[row][i]=Integer.valueOf(temp.substring(i,i+1));
}
col = temp.length();
row++;
}
System.out.println("Test:");
for(int i =0;i<row;i++) {
for(int j = 0;j<col;j++) {
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 1.采用regex(存储字符串) ,2.采用简单的模式匹配
// 进行行判断
for(int i =0;i<row;i++) {
for(int j= 0;j<col-3;j++) {// 减三个步长
if(maze[i][j]==2&&maze[i][j+1]==0&&maze[i][j+2]==2&&maze[i][j+3]==0)
{
sum+=1;
}
}
}
// 进行列判断
for(int i =0;i<col;i++) {
for(int j= 0;j<row-3;j++) {// 减三个步长
if(maze[j][i]==2&&maze[j+1][i]==0&&maze[j+2][i]==2&&maze[j+3][i]==0)
{
sum+=1;
}
}
}
// 进行斜行判断
// 往上走(若是正方形?)
// 斜对角线长度 为最大
// int link = Math.max(row, col);
// 预先估计,优化考虑任何情况!
// [0][i] 开始走
for(int i=0;i<col-3;i++) {
for(int j=0;j<row-3;j++) {
for(int k = i;k<col-3;k++) {
}
}
}
}
// 蛇形填数
// 矩阵,填充,i,j的关系变化
// 761
// 发散:矩阵回形填数等
void test3() {
int count=1;
int [][]maze = new int[100][100];
int row=0,col=0;
maze[row][col]=1;
while(row!=19||col!=19) { //当row
if(row==0) {
// count++;
// col++;
maze[0][++col]=++count;
}
while(col>0) {
maze[++row][--col]=++count;
if(row==19&&col==19) {
break;
}
}
if(col==0) {
maze[++row][0]=++count;
}
while(row>0) {
maze[--row][++col]=++count;
if(row==19&&col==19) {
break;
}
}
}
System.out.println("rs:"+count);
for(int i =0;i<=19;i++) {
for(int j =0;j<=19;j++) {
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
}
// 4.七段码
// 排列组合,判断是否连通
// 总的可能: 7 + C72 + C73 ..
// 1.硬数:全选和只选一个比较好弄,主要解决7选2-6的可能性
// 选一个:7 全选:1
// 选两个:2+2+2+1+2+1 需要去重,从a-f进行组合
// 选三个:(ab+)3 +(af+)2+ (bc+)2..
// 容易出错,,最好excel进行列举出来
// 2.编程方式
// 临链表存储相关连接,先排列组合然后两两判断是否连通》??
// 排序
// 寻找排序最短的字符串
// 6.成绩分析 15分
// Java基础编程应用,精度问题
void test5() {
int n;
Scanner scan = new Scanner(System.in);
List<Integer> list= new ArrayList<>();
n = Integer.valueOf(scan.nextLine());
for(int i=0;i<n;i++) {
list.add(Integer.valueOf(scan.nextLine()));//nextInt()??
}
IntSummaryStatistics iss = new IntSummaryStatistics();
// 思考intValue?
iss=list.stream().mapToInt(Integer::intValue).summaryStatistics();
BigDecimal bd = new BigDecimal(iss.getAverage());
float ave = bd.setScale(2,BigDecimal.ROUND_HALF_DOWN).floatValue();//确定half_up与half_down的区别
// 整除之后,不会填充0
System.out.println(iss.getMax()+"\n"+iss.getMin()+"\n"+ave);
}
// 7.单词分析
// 字符串处理 20分
// 统计词频,map记录,集合的应用
void test7() {
String inp = null;
Scanner scan = new Scanner(System.in);
inp=scan.nextLine();
Map<Character,Integer> wordMap = new HashMap<>();
for(int i =0;i<inp.length();i++)
{
// 若该键获取为空则计数为1否则加1
wordMap.compute(inp.charAt(i), (k,v)->v==null?1:v+1);
}
int max = 0;
// Character maxCC = null;
// 确定最大值
for(Entry<Character,Integer> e:wordMap.entrySet()) {
if(e.getValue()>max) {
//maxC.add(e.getKey());
// maxCC = e.getKey();
max = e.getValue();
}
}
List<Character> maxC = new ArrayList<>();// 考虑字典数最小那个,因此是一个集合??
for(Entry<Character,Integer> e:wordMap.entrySet()) {
if(e.getValue()==max) {
maxC.add(e.getKey());
// maxCC = e.getKey();
// max = e.getValue();
}
}
Collections.sort(maxC,new Comparator<Character>() {
@Override
public int compare(Character o1, Character o2) {
// TODO Auto-generated method stub
return o1-o2;
}
});
System.out.print(maxC.get(0)+"\n"+max);
}
// 8数字三角形 20分
// 树,贪心算法,
// 思路选取左右最大的即可回退加
// 约束,向左和向右相差不能超过1
void test8() {
// 输入构造
int n;
Scanner scan = new Scanner(System.in);
n = Integer.valueOf(scan.nextLine());
int [][]angle=new int[n][n];
// 构造三角形
for(int i=0;i<n;i++) {
for(int j= 0;j<i+1;j++) {
angle[i][j]=Integer.valueOf(scan.nextInt());
}
}
// 进行倒退,贪心处理
for(int i=n-2;i>=0;i--) {
for(int j=0;j<i+1;j++) {
angle[i][j]+=Math.max(angle[i+1][j], angle[i+1][j+1]);
}
}
System.out.println(angle[0][0]);
}
// 9子串分值和
// 字符串处理+复杂遍历计数
int f9(String str) {
// 使用集合
// 底层时间复杂度??
Set<Character> set = new HashSet<>();
for(int i=0;i<str.length();i++) {
set.add(str.charAt(i));
}
return set.size();
}
void test9() {
int count=0;
Scanner scan = new Scanner(System.in);
String inp = scan.nextLine();
for(int i=0;i<inp.length();i++) {
for(int j=i+1;j<inp.length()+1;j++) {
count+=f9(inp.substring(i,j));
}
}
System.out.print(count);
}
// 装饰珠
// 25
// 模拟,综合性
// 读懂题
// 6件装备: 可以镶嵌珠子,可以小于自身等级
// M种珠子:
// 抽象成类
// 默认六件装备
class Equipment{
int num;// 珠孔的数量
int[]level = new int[num];// 对应珠孔的等级(1-4)
}
int p;// 珠子的种类 1-4
// => 两件物品应该抽象两个类
// p种
class Node{ //珠子
int no;//序号(种类),其实利用数组下标也可以(并非对应,,可能打乱)
int num;// 珠子数量(上限,1-7)
int []weight = new int[num];//对应的数量的最大值,0对应1记得指标转换
}
void test10() {
int[] ELevels = new int[4];// 最大四级
for(int i =0;i<4;i++) {
ELevels[i]=0;
}
Scanner scan = new Scanner(System.in);
Equipment[] es= new Equipment[6];// 声明六个对象
// 构造装备
for(int i = 0;i<6;i++) {
es[i].num =scan.nextInt();
for(int j=0;j<es[i].num;j++) {
es[i].level[j]=scan.nextInt();
ELevels[es[i].level[j]-1]++;//下标对应
}
}
// 构造珠子
p = scan.nextInt();
Node[] nodes = new Node[p];
for(int i =0;i<p;i++) {
nodes[i].no = scan.nextInt();
nodes[i].num =scan.nextInt();
for(int j = 0;j<nodes[i].num;j++) {
nodes[i].weight[j]=scan.nextInt();
}
}
// 开始计算最大,,,最优解DP问题?贪心
// 六个装备放在一起算,,
// => 只需记录总的珠孔各等级数量
// 然后进行选择,
// 如何选择呢??先用少量用例进行试探
// level1:4个,level2:3个,level3:1个 level4:0个
// 下标分别对应0-3
// level高的可以变成低的,虽然单个价值可能不如低等级,但是可能存在低等级数量多,再多一个变化就很大
// 1.进行暴力解决?,level1肯定只能选择1了,如果level的数量大于最大值了,就已经没有添加的必要了。
// 按照优先填满低等级的策略进行?然后打表找到最高,,DP
// 应该倒着想,,低等级存在不确定,但高等级就不会,考虑高等级给出与不给出的价值变化
// (1)简化抽象问题
// 算法level对应数量
// 1-4级珠孔对应数量
// for(int i =0;i<6;i++) {
// //输入时就构造,无需再。。。
// }
//
// ELevels[i] 则对应等级i+1珠孔的数量
// 思考使用状态方程DP记录》?
int sum=0;
for(int i =3;i>=0;i--) {
if(ELevels[i]==0) {
continue;//该等级无珠孔
}else {
int curVal;
// 对比此等级给此等级以下的
if(ELevels[i]>=nodes[i].num) {// 先默认,序号按1,2,这样来的
curVal = nodes[i].weight[nodes[i].num-1];
}
//当前价值
else {
curVal = nodes[i].weight[ELevels[i]];
}
}
}
}
public static void main(String[]args) {
TestB testB = new TestB();
testB.test9();
}
}
2019年B组
package lq.questions.province_2019;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @AUTHOR LYF
* @DATE 2021/4/7
* @VERSION 1.0
* @DESC
* 2019年B组
*/
public class TestB {
// 组队
// 求最大首发队员和
// 直接计算,无需
// 490
void test1(){
}
// 不同子串
// 遍历
// 88
void test2(){
// 15位
String str = "010011000101001";
// 遍历组合,加入set
Set<String> set = new HashSet<>();
for(int i=0;i<str.length();i++){
for(int j = i;j<str.length();j++){
set.add(str.substring(i,j+1));
}
}
System.out.println("res:"+set.size());
set.stream().forEach(System.out::println);
}
// 数列求值
//4659
void test3(){
int a=1,b=1,c=1;
for(int i =4;i<=20190324;i++){
int temp1=c;
c = (a+b+c)%10000;
a=b%10000;
b=temp1%10000;
}
System.out.println(c);
// /* System.out.println(f(2019));*/
}
boolean judge(int n){
String str = n+"";
for(int i = 0;i<str.length();i++){
if (str.charAt(i)=='2'||str.charAt(i)=='4')
return false;
}
return true;
}
// 数的分解
// 2019进行3个
// 40785
void test4(){
int count= 0;
for(int i =1999;i>=1009;i--){
int sum = 2019-i;
//分解sum
for(int j =1;j<sum;j++){
if(judge(j)&&judge(sum-j)){
count++;
}
}
}
System.out.println(count);
}
boolean f6(int n){
String str = n+"";
Set<Character> set = new HashSet<>();
set.add('2');
set.add('0');
set.add('1');
set.add('9');
for(int i =0;i<str.length();i++){
if(set.contains(str.charAt(i))){
return true;
}
}
return false;
}
// 特别数之和
void test6(){
Scanner scanner = new Scanner(System.in);
int val=scanner.nextInt();
int sum=0;
for(int i =1;i<=val;i++){
if(f6(i)){
sum+=i;
}
}
System.out.println(sum);
}
// 外卖优先级
// 模拟题
// N (店家数量,时间,编号有一个订单)
// 考虑会不会存在同时具有订单,肯定可以的啊
// 进行时间遍历,排序
class TimeNode implements Comparator<TimeNode>{
private int time;//时刻
private int no;//序号
@Override
public int compare(TimeNode o1, TimeNode o2) {
return o1.time-o2.time;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public void setTime(int time) {
this.time = time;
}
public int getTime() {
return time;
}
}
void test7(){
int n,m,t;// n 编号,m 订单信息,t时间
Map<Integer,Integer> cache = new HashMap<>();//店家编号以及优先级
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
int [] merchant = new int[n];// 编号-1才是对应的
Arrays.fill(merchant,0); // 优先级
m = scan.nextInt();
t = scan.nextInt();
List<TimeNode> list = new ArrayList<>();
for(int i =0;i<m;i++){
TimeNode node = new TimeNode();
node.setTime(scan.nextInt());
node.setNo(scan.nextInt());
list.add(node);
}
Collections.sort(list, new Comparator<TimeNode>() {
@Override
public int compare(TimeNode o1, TimeNode o2) {
return o1.getTime()- o2.getTime();
}
});
System.out.println("test:");
list.stream().forEach(System.out::print);
for(TimeNode node:list){
}
}
// 人物相关性分析
void test8(){
Scanner scanner = new Scanner(System.in);
String str2 =scanner.nextLine();
int k = Integer.valueOf(str2);
String str = scanner.nextLine();
// 法1直接使用find方法记录位置
// regex
String regexAlice = "Alice";
String regexBob = "Bob";
Pattern pattern = Pattern.compile(regexAlice);
Pattern pattern1 = Pattern.compile(regexBob);
Matcher matcher = pattern.matcher(str);
Matcher matcher1 = pattern1.matcher(str);
List<Integer> listAlice = new ArrayList<>();
List<Integer> listBob = new ArrayList<>();
while(matcher.find()){
listAlice.add(matcher.start()); //开始index
// System.out.print(matcher.start());
// System.out.print(matcher.group()+"->");
}
while(matcher1.find()){
listBob.add(matcher1.start()); //开始index
// System.out.print(matcher1.group()+"->");
}
int count=0;
for(int i =0;i<listAlice.size();i++){
// 寻找邻近是否有Bob
int index= listAlice.get(i);
for(int j = 0;j<listBob.size();j++){
if(Math.abs(listBob.get(j)-index)<=k){
count++;
}
}
}
System.out.println(count);
}
// 后缀表达式
// 大数问题?? 看测评数据
void test9(){
int n,m;
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
m = scan.nextInt();
List<Integer> numbers = new ArrayList<>();
for(int i = 0;i<n+m+1;i++){
numbers.add(scan.nextInt());
}
numbers.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
// System.out.println("测试");
// numbers.stream().forEach((e)->{
// System.out.print(e+"->");
// });
int sum = 0;
for(int i = 0 ;i<numbers.size();i++){
if(i<=n){
sum+=numbers.get(i);
}else {
sum-=numbers.get(i);
}
}
System.out.println(sum);//"res:"+
}
public static void main(String[]args){
TestB testB = new TestB();
testB.test9();
}
}