2020蓝桥杯简单题讲解

发布于 2022-01-12  452 次阅读


image-20220112065800917

#include<iostream>
using namespace std;
int main(){
    int ans=0;
    for(int i=1;i<=2020;i++){
        int nowNumber=i;
        while(nowNumber!=0){
            int temp=nowNumber%10;
            if(temp==2){
                ans++;
            }
            nowNumber=nowNumber/10;
        }
    }
    cout<<ans;
    // 2020
    // 2020%10=0
    // 2020/10=202%10=2;
    // 2020/100=20%10=0;
    // 2020/1000=2%10=2;
    // 2020/10000=0
}

答案624


image-20220112070936190

#include<iostream>
using namespace std;
int gcd(int a,int b){
    if(a%b==0){
        return b;
    }else{
        return gcd(b,a%b);
    }
}
int main(){
    int ans=0;
    int i,j;//分别记录分子分母
    for(i=1;i<=2020;i++){
        for(j=1;j<=2020;j++){
            if(gcd(i,j)==1){
                ans++;
            }
        }
    }
    cout<<ans;
}

答案:2481215


image-20220112071015187

#include<iostream>
using namespace std;
int map[50][50];
int main(){
    int now=1;
    for(int i=1;i<50;i++){
        int x,y;
        if(i%2==1){
            x=1;
            y=i;
            for(int j=1;j<=i;j++){
                map[x][y]=now;
                now++;
                x++;
                y--;
            }
        }else{
            x=i;
            y=1;
            for(int j=1;j<=i;j++){
                map[x][y]=now;
                now++;
                x--;
                y++;
            }
        }
    }
    for(int i=1;i<=20;i++){
        for(int j=1;j<=20;j++){
            cout<<map[i][j]<<"\t";
        }
        cout<<endl;
    }
    cout<<map[20][20];

}

答案:761


image-20220112071049572

答案:80

#include<iostream>
using namespace std;
// A B C D E F G
// 0 1 2 3 4 5 6
int list[20][2]={{0,1},{0,5},{1,0},{1,6},{1,2},{2,1},{2,6},{2,3},{3,2},
{3,4},{4,3},{4,6},{4,5},{5,0},{5,6},{5,4},{6,5},{6,1},{6,2},{6,4}};
int fa[7];
int init(){
    for(int i=0;i<7;i++){
        fa[i]=i;
    }
}
int find(int x){
    if(fa[x]==x){
        return x;
    }
    return find(fa[x]);
}
void Union(int x,int y){
    int x_root=find(x);
    int y_root=find(y);
    fa[y_root]=x_root;
}
int count_sameroot(int x){
    int ans=0;
    for(int j=0;j<7;j++){
        if(find(j)==x){
            ans++;
        }
    }
    return ans;
}
int main(){
    int ans=0;
    int map[7];
    for(int i=1;i<=127;i++){
        int temp=i;
        int Count=0;
        for(int j=0;j<7;j++){
            map[j]=temp%2;
            if(map[j]==1){
                Count++;
            }
            temp=temp/2;
        }
        init();
        for(int q=0;q<7;q++){
            if(map[q]==1){
                for(int t=0;t<7;t++){
                    if(map[t]==1&&t!=q){
                        // 对应关系,t,q都亮了
                        for(int j=0;j<20;j++){
                            if(list[j][0]==t&&list[j][1]==q){
                                // t,q可连接
                                Union(t,q);
                            }
                        }
                    }
                }
            }
        }
        int max=0;
        for(int j=0;j<7;j++){
            if(max<count_sameroot(j)){
                max=count_sameroot(j);
            }
        }
        if(Count==max){
            ans++;
            char graph[3][3];
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    graph[j][k]=' '; 
                }
            }
            if(map[0]==1){graph[0][1]='_';}
            if(map[1]==1){graph[1][2]='|';}
            if(map[2]==1){graph[2][2]='|';}
            if(map[3]==1){graph[2][1]='_';}
            if(map[4]==1){graph[2][0]='|';}
            if(map[5]==1){graph[1][0]='|';}
            if(map[6]==1){graph[1][1]='_';}
            for(int j=0;j<3;j++){
                for(int k=0;k<3;k++){
                    cout<<graph[j][k];
                }
                cout<<endl;
            }
            cout<<"-----------"<<endl;
        }
    }
    cout<<ans;
}

image-20220112071117920

#include<cstdio>
int main(){
    int n;
    scanf("%d",&n);
    int max=-1;
    int min=101;
    int sum=0;
    for(int i=0;i<n;i++){
        int temp;
        scanf("%d",&temp);
        sum=sum+temp;
        if(max<temp){
            max=temp;
        }
        if(min>temp){
            min=temp;
        }
    }
    double ave=sum*1.0/n*1.0;
    printf("%d\n%d\n%.2lf",max,min,ave);
}

image-20220112071149341

#include<iostream>
using namespace std;
int main(){
    int nowdate;
    cin>>nowdate;
    int year=nowdate/10000;
    int line1flag=0;
    int line2flag=0;
    int output1,output2;
    int RMonthDays[12]={31,29,31,30,31,30,31,31,30,31,30,31};    
    int PMonthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    for(int i=year;(line1flag!=1||line2flag!=1);i++){
        int temp=i;
        int a=temp%10;
        int b=temp/10%10;
        int c=temp/100%10;
        int d=temp/1000;
        int mayMonth=a*10+b;
        if(!(mayMonth>=0&&mayMonth<=12)){
            continue;
        }
        int mayDay=c*10+d;
        if((year%4==0&&year%100!=0)||year%400==0){
            if(!(mayDay>=1&&mayDay<=RMonthDays[mayMonth-1])){
                continue;
            }
        }else{
            if(!(mayDay>=1&&mayDay<=PMonthDays[mayMonth-1])){
                continue;
            }
        }
        int maydate=temp*10000+mayMonth*100+mayDay;
        if(maydate>nowdate&&line1flag==0){
            line1flag=1;
            output1=maydate;
        }
        if(maydate>nowdate&&line2flag==0&&a==c&&b==d){
            line2flag=1;
            output2=maydate;
        }
    }
    cout<<output1<<endl<<output2;
}

image-20220112071221959

#include<iostream>
#include<string>
using namespace std;
int main(){
    string s;
    cin>>s;
    int ans=0;
    for(int i=0;i<s.size();i++){
        for(int j=1;j<=s.size()-i;j++){
            string nowString=s.substr(i,j);
            int a[26]={0};
            for(int k=0;k<nowString.size();k++){
                if(a[nowString[k]-'a']==0){
                    ans++;
                }else if(a[nowString[k]-'a']==1){
                    ans--;
                }
                a[nowString[k]-'a']++;
            }
        }
    }
    cout<<ans;

}

不会写珂朵莉树的废柴ACMer