一、前言
接著上一篇藍橋杯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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/275805.html
標籤:其他
上一篇:JavaScript基礎入門 續
