2021第十二届蓝桥杯国赛C/C++大学B组题解

时间:2023-03-14 15:18:01 买帖  | 投诉/举报

篇首语:本文由小编为大家整理,主要介绍了2021第十二届蓝桥杯国赛C/C++大学B组题解相关的知识,希望对你有一定的参考价值。

A题 宽带

/*1Byte=8bit1KByte=1024Byte1M=1024KByte1MB/s=8Mbps200Mbps=25MB/s*/

B题 纯质数

/**1903*/#include <bits/stdc++.h>using  namespace std;const int maxn = 20210610;int f[maxn];// 0是质数 1是非质数void init(){    f[1]=1,f[0]=1;    for(int i=2;i<maxn;i++){        if(f[i]==0){            for(int j=2;j*i<maxn;j++){                f[i*j]=1;            }        }    }}bool check(int x){    while(x>0){        if(f[x%10]){            return false;        }        x/=10;    }return true;}int main(){    int ans=0;    init();//    for(int i=2;i<=20210605;i++){        if(f[i]==0){//如果是一个质数            if(check(i)){                cout<<i<<endl;                ans++;            }        }    }    cout<<ans<<endl;return 0;}

C题 完全日期

/**977**/#include <bits/stdc++.h>using namespace std;const int maxn=1e5*2+5;int f[maxn];bool Run(int year){    if( (year%4==0 && year%100!=0) || (year%400==0) ){        return true;    }    return false;}bool check(int y,int m,int d){    int sum=0;    while(y>0){        sum+=y%10;        y/=10;    }    while(m>0){        sum+=m%10;        m/=10;    }    while(d>0){        sum+=d%10;        d/=10;    }    if(f[sum]==1){        return true;    }    return false;}void init(){    f[1]=1;    for(int i=2;i*i<maxn;i++){        f[i*i]=1;    }}int main(){//从2001.1.1到2021.12.31 // 平年2月 28天 闰年29天    init();    int ans=0;    for(int i=2001;i<=2021;i++){//year        int m[13]={0,//m        31,//1        28,//2        31,//3        30,//4        31,//5        30,//6        31,//7        31,//8        30,//9        31,//10        30,//11        31//12        };        if(Run(i)){//如果是闰年            m[2]++;        }        for(int j=1;j<=12;j++){//month            for(int k=1;k<=m[j];k++){                if(check(i,j,k)){                    cout<<"年"<<i<<"月"<<j<<"日"<<k<<endl;                    ans++;                }            }        }    }    cout<<ans<<endl;return 0;}

D题 最小权值

/**2597854139*/#include <bits/stdc++.h>using namespace std;const int maxn=2022;#define ll long longll f[maxn],w[maxn];void get(int c,int x,int l,int r){    if(x>2021)return ;    if(l<=2021){        f[c]++;        get(c,l,l*2,l*2+1);    }    if(r<=2021){        f[c]++;        get(c,r,r*2,r*2+1);    }}int main(){    for(int i=1;i<=2021;i++){        f[i]=0,w[i]=0;    }    for(int i=1;i<=2021;i++){        get(i,i,i*2,i*2+1);//当前结点 左结点 右结点    }    for(int i=2021;i>=1;i--){        if(2*i>2021 && 2*i+1>2021){            w[i]=0;            continue;        }        w[i]=1;        int mark=0;        if(i*2<=2021){//左结点            w[i]+=2*w[i*2];            mark++;        }        if(i*2+1<=2021){//右结点            w[i]+=3*w[i*2+1];            mark++;        }        if(mark==2){            w[i]+=(f[i*2]*f[i*2])*f[i*2+1];        }    }    cout<<w[1]<<endl;return 0;}

E题 大写

#include <bits/stdc++.h>using namespace std;int main(){    string s;    while(cin>>s){        for(int i=0;i<s.length();i++){            if(s[i]>='a'&&s[i]<='z'){                s[i]-=32;            }            cout<<s[i];        }cout<<endl;    }return 0;}

F题 123

//前缀和数组水数据#include <bits/stdc++.h>using namespace std;#define ll long longconst  int maxn=1e8+5;int a[maxn],w[maxn];int main(){    int k=1,d=1;    for(int i=1;i<maxn;i++){        if(d<k){a[i]=d;d++;        }else{i--;d=1;k++;        }    }    w[1]=a[1];    for(int i=2;i<maxn;i++){        w[i]=w[i-1]+a[i];    }    int t;    scanf("%d",&t);    int x,y;    while(t--){        scanf("%d %d",&x,&y);        if(x==y){            printf("%d\\n",a[x]);        }else{            printf("%d\\n",w[y]-w[x]+a[x]);        }    }return 0;}

G题 异或变换

//模拟水数据#include <bits/stdc++.h>using namespace std;int main(){    int n,t;    int x,y;    while(scanf("%d %d",&n,&t)!=EOF){        string s,b;        cin>>s;        b=s;        while(t--){            for(int i=1;i<s.length();i++){                x=s[i-1]-'0';                y=s[i]-'0';                x=x^y;                b[i]=x+'0';            }            s=b;        }        cout<<s<<endl;    }return 0;}

H题 二进制问题

//位运算水数据#include <bits/stdc++.h>using namespace std;int main(){    int n,k;    while(scanf("%d %d",&n,&k)!=EOF){        int sum=0,ans,j;        for(int i=1;i<=n;i++){            ans=0,j=i;            while(1){                if( j&1 ){                    ans++;                }                j= j >> 1;                if(j==0)break;            }            if(ans==k){                sum++;            }        }        printf("%d\\n",sum);    }return 0;}

I题 翻转括号序列

//模拟水数据#include <bits/stdc++.h>using namespace std;string s;void rev(int l,int r){    for(int i=l;i<=r;i++){        if(s[i]=='('){            s[i]=')';        }else{            s[i]='(';        }    }}int check(int l){    if(s[l]==')')return 0;    int left = 1,right = 0,mark=0;    for(int i=l+1;i<s.length();i++){        if(left<right)return i-1;        if(s[i]=='('){            left++;        }        if(s[i]==')'){            right++;        }        if(left==right){            mark=i+1;        }    }    return mark;}int main(){    int n,m;    while(scanf("%d %d",&n,&m)!=EOF){        cin>>s;        while(m--){            int op,l,r;            scanf("%d",&op);            switch(op){                case 1:                    scanf("%d %d",&l,&r);                    rev(l-1,r-1);                    break;                case 2:                    scanf("%d",&l);                    printf("%d\\n",check(l-1));                    break;            }        }    }return 0;}

J题 异或三角

//三层for强行爆破#include <bits/stdc++.h>using namespace std;#define ll long longbool check(int i,int j,int k){    if(i+j<=k || i+k<=j || j+k<=i ){        return false;    }    int temp = i^j;    temp = temp^k;    if(!temp)return true;return false;}int main(){    int T;    scanf("%d",&T);    ll n;    while(T--){        cin>>n;        ll ans=0;        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++){                for(int k=1;k<=n;k

以上是关于2021第十二届蓝桥杯国赛C/C++大学B组题解的主要内容,如果未能解决你的问题,请参考以下文章