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

A题 宽带

在这里插入图片描述

/*1Byte=8bit
1KByte=1024Byte
1M=1024KByte
1MB/s=8Mbps
200Mbps=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++){//yearint m[13]={0,//m31,//128,//231,//330,//431,//530,//631,//731,//830,//931,//1030,//1131//12};if(Run(i)){//如果是闰年m[2]++;}for(int j=1;j<=12;j++){//monthfor(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 long
ll 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 long
const  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 long
bool 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++){if(check(i,j,k)){ans++;}}}}cout<<ans<<endl;}
return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部