藍橋杯歷年真題及決議.
目錄
- 藍橋杯歷年真題及決議.
- A: 門牌制作(難度:★)
- 思路:
- 代碼:
- B: 尋找 2020(難度:★★)
- 思路:
- 代碼:
- C: 蛇形填數(難度:★★★)
- 思路:
- 代碼:
- D: 七段碼(難度:★★★)
- 思路:
- 代碼:
- E: 排序(難度:★★★★)
- 思路:
- 代碼:
- F: 成績分析(難度:★)
- 思路:
- 代碼:
- G: 單詞分析(難度:★★)
- 思路:
- 代碼:
- H: 數字三角形(難度:★★★★)
- 思路:
- 代碼:
- I: 子串分值和(難度:★★★★★)
- 思路:
- 代碼:
- J: 裝飾珠(難度:★★★★★)
- 思路:
- 代碼:
A: 門牌制作(難度:★)
思路:
624
從1~2020開始回圈,一直計算即可,
代碼:
public class A {
public static void main(String[] args) {
int ans=0;
for(int i=1;i<=2020;i++){
int x=i;
while(x>0){
if(x%10==2)ans++;
x/=10;
}
}
System.out.println(ans);
}
}
B: 尋找 2020(難度:★★)
思路:
16520
定義二維矩陣,遍歷每個坐標,以每個坐標為起點分別向右向下向右下三個方向遍歷,
代碼:
沒有題目和測驗資料,有了再寫
C: 蛇形填數(難度:★★★)
思路:
761
由規律推公式
(n-1)(2n-1)+n
帶入n=20得結果,
代碼:
public class C {
public static void main(String[] args) {
int n=20;
System.out.println((n-1)*(2*n-1)+n);
}
}
D: 七段碼(難度:★★★)
思路:
80
列舉127種字符組合,挨個查看是否聯通,
代碼:
DFS生成127個字符組合
E: 排序(難度:★★★★)
思路:
jonmlkihgfedcba
冒泡排序,要求字串最短,那就假設完全逆序,
設長度為n,則移動次數為 n*(n-1)/2
要求移動次數恰好大于100,則 n=15;移動次數105
要求字典序最小,則把第六個字符移動到第一個位置,前五個字符后移一位,
代碼:
純邏輯推導,無代碼
F: 成績分析(難度:★)
思路:
純比較大小,沒啥說的
代碼:
import java.util.Scanner;
public class F {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
double sum=0;
for(int i=0;i<n;i++){
int t=scanner.nextInt();
min=Math.min(min, t);
max=Math.max(max, t);
sum+=t;
}
System.out.println(max+"\n"+min+"\n"+String.format("%.2f", sum/n));
}
}
G: 單詞分析(難度:★★)
思路:
記錄每個字符的出現次數即可
代碼:
import java.util.Scanner;
public class G {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
char c[]=scanner.next().toCharArray();
int buf[]=new int[26];
for(int i=0;i<c.length;i++){
buf[c[i]-'a']++;
}
int val=buf[0],index=0;
for(int i=0;i<26;i++){
if(buf[i]>val){
val=buf[i];
index=i;
}
}
System.out.println((char)('a'+index)+"\n"+val);
}
}
H: 數字三角形(難度:★★★★)
思路:
DP推導+奇偶判斷
代碼:
import java.util.Scanner;
public class H {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int arr[][]=new int[n+1][n+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
arr[i][j]=scanner.nextInt();
arr[i][j]+=Math.max(arr[i-1][j-1], arr[i-1][j]);
}
}
System.out.println(n%2==1?arr[n][n/2+1]:Math.max(arr[n][n/2], arr[n][n/2+1]));
}
}
I: 子串分值和(難度:★★★★★)
思路:
寫了個兩層暴力,for回圈,時間復雜度n^2
代碼:
import java.util.*;
public class I {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String string=scanner.next();
char c[]=string.toCharArray();
long ans=0;
for(int i=0;i<c.length;i++){
HashSet<Character> set=new HashSet<Character>();
for(int j=i;j<c.length;j++){
set.add(c[j]);
ans+=set.size();
}
}
System.out.println(ans);
}
}
J: 裝飾珠(難度:★★★★★)
思路:
代碼:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/183445.html
標籤:其他
下一篇:java基礎打卡
