藍橋杯歷年真題及決議.
目錄:
- 藍橋杯歷年真題及決議.
- A:門牌制作(難度:★)
- 題目:
- 分析:
- 代碼:
- B:既約分數(難度:★★)
- 題目:
- 分析:
- 代碼:
- C:蛇形填數(難度:★★)
- 題目:
- 分析:
- 代碼:
- D:七段碼(難度:★★★)
- 題目:
- 分析:
- 代碼:
- E:平面分割(難度:★★★★)
- 題目:
- 分析:
- 代碼:
- F:成績分析(難度:★)
- 題目:
- 分析:
- 代碼:
- G:回文日期(難度:★★★)
- 題目:
- 分析:
- 代碼:
- H:子串分值(難度:★★★★)
- 題目:
- 分析:
- 代碼:
- I:荒島探測(難度:★★★★★)
- 題目:
- 分析:
- 代碼:
- J:字串排序(難度:★★★★★)
- 題目:
- 分析:
- 代碼:
- 演算法交流群
A:門牌制作(難度:★)
題目:

分析:
for回圈模擬計算程序即可,
答案=624
代碼:
public class A門牌制作 {
public static void main(String[] args) {
int ans=0;
for(int i=1;i<=2020;i++){
int temp=i;
while(temp!=0){
if(temp%10==2)ans++;
temp/=10;
}
}
System.out.println(ans);
}
}
B:既約分數(難度:★★)
題目:

分析:
列舉所有分子分母,求GCD,最終去重,
剩余的個數即為結果,
答案=2481215
代碼:
import java.util.HashSet;
public class B既約分數 {
public static int gcd(int x,int y){
if(y==0)return x;
else return gcd(y,x%y);
}
public static void main(String[] args) {
int ans=0;
HashSet<Integer> set[]=new HashSet[2021];
for(int i=1;i<=2020;i++){
set[i]=new HashSet<Integer>();
for(int j=1;j<=2020;j++){
int t=gcd(i,j);
set[i/t].add(j/t);
}
}
for(int i=1;i<=2020;i++){
ans+=set[i].size();
}
System.out.println(ans);
}
}
C:蛇形填數(難度:★★)
題目:

分析:
根據資料找規律,
把矩陣看作從左下角到右上角,或者右上角到左下角的斜線,
那么,第n行,第n列,所對應的斜線為第2n-1條,
說明有2n-2條完整的斜線,每條斜線長度從一開始遞增,每次+1,
即完整斜線上所有的數字總數為( 1 + 2 * n - 2 ) * ( 2 * n - 2 ) / 2;
剩下半條斜線上有n個數字,
即公式為( 1 + 2 * n - 2 ) * ( 2 * n - 2 ) / 2 + n;
答案=761
代碼:
public class C蛇形填數 {
public static void main(String[] args) {
int n=20;
System.out.println((1+2*n-2)*(2*n-2)/2+n);
}
}
D:七段碼(難度:★★★)
題目:

分析:
七個塊,當作二進制進行列舉,一共128種可能,挨個找即可,
答案=80
代碼:
public class D七段碼 {
public static void main(String[] args) {
System.out.println(80);
//手查的
}
}
E:平面分割(難度:★★★★)
題目:

分析:
//待更新
代碼:
F:成績分析(難度:★)
題目:



分析:
按照題目所說進行模擬運算即可,
代碼:

import java.util.Scanner;
public class F成績分析 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int max=0,min=Integer.MAX_VALUE;
double sum=0;
for(int i=0;i<n;i++){
int t=sc.nextInt();
max=Math.max(max, t);
min=Math.min(min, t);
sum+=t;
}
System.out.println(max);
System.out.println(min);
System.out.println(String.format("%.2f", sum/n));
}
}
G:回文日期(難度:★★★)
題目:



分析:
將時間獲取到,分割為年月日,
三層回圈嵌套時間列舉,對每種情況檢查,符合條件即記錄,找到答案結束程式即可,
代碼:

import java.util.Scanner;
public class G回文日期 {
public static int tian[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
public static void isr(int x){
tian[2]=((x%4==0&&x%100!=0)||(x%400==0))?29:28;
}
public static boolean check(String s){
return s.equals(new StringBuilder(s).reverse().toString());
}
public static boolean check2(String s){
return s.charAt(0)==s.charAt(2)&&s.charAt(0)==s.charAt(5)&&s.charAt(0)==s.charAt(7)
&&s.charAt(1)==s.charAt(3)&&s.charAt(1)==s.charAt(4)&&s.charAt(1)==s.charAt(6);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int year=n/10000,month=(n%10000)/100,day=n%100;
boolean first=true,line1=false,line2=false;
String str1="",str2="";
for(int i=year;i<=9999;i++){
isr(i);
for(int j=first?month:1;j<=12;j++){
for(int k=first?day:1;k<=tian[j];k++){
if(first){
first=false;
continue;
}
if(!line1&&check(i+""+(j<10?"0"+j:j)+""+(k<10?"0"+k:k))){
str1=i+""+(j<10?"0"+j:j)+""+(k<10?"0"+k:k);
line1=true;
}
if(!line2&&check2(i+""+(j<10?"0"+j:j)+""+(k<10?"0"+k:k))){
str2=i+""+(j<10?"0"+j:j)+""+(k<10?"0"+k:k);
line2=true;
}
if(line1&&line2){
System.out.println(str1);
System.out.println(str2);
return ;
}
}
}
}
}
}
H:子串分值(難度:★★★★)
題目:



分析:
我們可以通過計算每個字母所產生的貢獻進行計算,觀察發現,
某字母x與左邊x的距離乘以與右邊x的距離即為該字母產生的貢獻,
記錄左邊距離left,記錄右邊距離right
ans=Σ(left[i]*right[i])
代碼:

import java.util.Scanner;
public class H子串分值 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
char c[]=sc.next().toCharArray();
int left[]=new int[c.length];
int right[]=new int[c.length];
int buf[]=new int[26];
for(int i=0;i<26;i++)buf[i]=-1;
for(int i=0;i<c.length;i++){
left[i]=i-buf[c[i]-'a'];
buf[c[i]-'a']=i;
}
for(int i=0;i<26;i++)buf[i]=c.length;
for(int i=c.length-1;i>=0;i--){
right[i]=buf[c[i]-'a']-i;
buf[c[i]-'a']=i;
}
long ans=0;
for(int i=0;i<c.length;i++){
ans+=left[i]*right[i];
}
// System.out.println(Arrays.toString(left));
// System.out.println(Arrays.toString(right));
System.out.println(ans);
}
}
I:荒島探測(難度:★★★★★)
題目:
//無題源
分析:
代碼:
J:字串排序(難度:★★★★★)
題目:
//無題源
分析:
代碼:
演算法交流群

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276733.html
標籤:其他
上一篇:Zookeeper 完全分布安裝(撰寫shell腳本啟動,附帶安裝包)
下一篇:資料清洗
