第十一届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组

第十一届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组

文章目录

1、字串排序


c 复制代码
// 转载博客链接
https://blog.csdn.net/weixin_46266058/article/details/123469287

2、门牌制作

c 复制代码
#include<iostream>

#define ll long long
using namespace std;
int ans;
void getsi(int x){
	while(x){
		int t=x%10;
		if(t==2)ans++;
		x/=10;
	}
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	for(int i=1;i<=2020;i++){
		getsi(i);
	}
	cout<<ans;
	return 0;
}

3、既约分数

读懂题意,不然写完了才发现是错的。

c 复制代码
#include<iostream>
#include<set>
#define ll long long
using namespace std;
int gcd(int a,int b){
	if(a%b==0)return b;
	return gcd(b,a%b);
}
int ans;
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	for(int i=1;i<=2020;i++)
		for(int j=1;j<=2020;j++){
			//if(i==j)continue;
			if(gcd(i,j)==1)ans++;
		}
	cout<<ans;
	return 0;
}

4、蛇形填数

c 复制代码
#include<iostream>
using namespace std;
const int N = 40; 
int arr[N][N];
int main(){
  	int r = 1;
  	for(int i=1;i<=N;i++){
  		if(i&1){
  			for(int x=i,y=1;x>=1&&y<=i;x--,y++){
  				arr[x][y]=r++;
			}	
		}else{
			for(int y=i,x=1;y>=1&&x<=i;y--,x++){
				arr[x][y]=r++;
			}
		}
  	}
	cout<<arr[20][20];	
  	return 0;
}

5、跑步锻炼

c 复制代码
#include<iostream>
using namespace std;
int ans;
int main(){
    int month[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
    int num = 0;//总共过了多少天,%7求周一
    for(int i=2000;i<=2020;i++){
    	if(i%100==0||(i%4==0&&i%100!=0))month[2]=29;
    	else month[2]=28;
    	for(int j=1;j<=12;j++){
    		for(int k=1;k<=month[j];k++){
    			// num%7 对应 0~6所以等于2的时候就是周一,k==1 表示初一 
    			if(k==1||num%7==2){
    				ans+=2;
				}else{
					ans+=1;
				}
				num++;
				if(i==2020&&j==10&&k==1){
					cout<<ans;
					return 0;
				}
			}
		}
	}
    return 0;
}

6、七段码

图的建立,并查集,搜索。

c 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
int use[N], ans, e[N][N], fa[N];
void init(){
	/*	
		连边建图,e[i][j] == 1表示第i段和第j段灯管相邻
		a b c d e f g
		1 2 3 4 5 6 7
	*/
	e[1][2]=e[1][6]=1;
	e[2][1]=e[2][3]=e[2][7]=1;
	e[3][2]=e[3][7]=e[3][4]=1;
	e[4][3]=e[4][5]=1;
	e[5][4]=e[5][7]=e[5][6]=1;
	e[6][1]=e[6][5]=e[6][7]=1;
	e[7][2]=e[7][3]=e[7][5]=e[7][6]=1;
}
int find(int x){
	return x==fa[x] ? x : (fa[x]=find(fa[x]));
}
void dfs(int d){
	if(d > 7){
		/* 并查集判是否在同一集合 */
		for(int i = 1;i <= 7;i++)fa[i] = i;//初始化父亲集合
		for(int i = 1;i <= 7;i++)
			for(int j = 1;j <= 7;j++)
				if(e[i][j] && use[i] && use[j]){//i和j相邻并且都亮着
					int fx = find(i),fy = find(j);
					if(fx != fy)fa[fx] = fy;//如果不在同一集合,合并
				}
		int k = 0;
		for(int i = 1;i <= 7;i++)
			if(use[i] && fa[i] == i)k++;
		if(k == 1)ans++;//如果所有亮灯都属于同一个集合
		return;
	}
	use[d] = 1;//打开d这个灯,继续开关下一个灯
	dfs(d + 1);
	use[d] = 0;//关闭d这个灯,继续开关下一个灯
	dfs(d + 1);
}
int main(){
	init();
	dfs(1);
	cout << ans;
}

7、成绩统计


c 复制代码
#include<iostream>
#include<stdio.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
int n,a[N];
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	int x=0,b=0;
	for(int i=1;i<=n;i++){
		if(a[i]>=60)x++;
		if(a[i]>=85)b++;
	}
	int a1=x*1000/n;
	int t1=a1%10;
	if(t1>=5)a1=a1/10+1;
	else a1=a1/10;
	int a2=b*1000/n;
	int t2=a2%10;
	if(t2>=5)a2=a2/10+1;
	else a2=a2/10;
	printf("%d%\n%d%\n",a1,a2);
	return 0;
}

8、回文日期




题这么长吓我一跳,没想到挺简单的。。。。

c 复制代码
#include<iostream>
#define ll long long
using namespace std;
int t1,t2,y1,y2,m1,m2,d1,d2,ans;
int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isy(int x){
	return x%400==0||(x%4==0&&x%100!=0);
}
bool isRve(int y,int m,int d){
	int a[]={y/1000,y/100%10,y/10%10,y%10,m/10,m%10,d/10,d%10};
	for(int i=0;i<4;i++){
		if(a[i]!=a[7-i])return false;
	}
	return true;
} 
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>t1>>t2;
	y1=t1/10000,y2=t2/10000;
	m1=t1%10000/100,m2=t2%10000/100;
	d1=t1-y1*10000-m1*100,d2=t2-y2*10000-m2*100;
//	cout<<y1<<" "<<m1<<" "<<d1<<endl;
//	cout<<y2<<" "<<m2<<" "<<d2;
	for(int year=y1;year<=y2;year++){
		for(int mon=(year==y1?m1:1);mon<=(year==y2?m2:12);mon++){
			int maxday=(mon==2&&isy(year))?29:d[mon];
			for(int day=(year==y1&&mon==m1?d1:1);day<=(year==y2&&mon==m2?d2:maxday);day++){
				if(isRve(year,mon,day)){
					ans++;
				}
			}
		}
	}
	cout<<ans;
	return 0;
}

9、子串分值和


直接暴力枚举,只能过40%。。。。

c 复制代码
#include<iostream>
#include<set>
#define ll long long
using namespace std;
string s;
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>s;
	int n=s.size();
	set<char> st;
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			for(int k=i;k<=j;k++){
				st.insert(s[k]);	
			}		
			ans+=st.size();
			st.clear();
		}
	}
	cout<<ans;
	return 0;
}

10、平面切分


骗了10%。。。。

c 复制代码
#include<iostream>
using namespace std;
int main(){
	int n;cin>>n;
	int a,b;
	for(int i=0;i<n;i++)cin>>a>>b;
	int ans=0;
	switch(n){
		case 0:ans=1;break;
		case 1:ans=2;break;
		case 2:ans=4;break;
		case 3:ans=7;break;
		case 4:ans=11;break;
	}
	cout<<ans;
	return 0;
}
相关推荐
Shadow(⊙o⊙)9 小时前
智能指针、循环引用、锁、删除器
开发语言·c++·后端·visual studio
水云桐程序员9 小时前
C++官方文档获取平台
c++·学习方法
leoufung9 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
Sylvia-girl9 小时前
C++模板【上】
开发语言·c++
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【哈夫曼贪心】:荷马史诗
c++·算法·贪心·csp·信奥赛·哈夫曼贪心·荷马史诗
爱编码的小八嘎9 小时前
C语言完美演绎9-6
c语言
样例过了就是过了9 小时前
LeetCode热题100 最小路径和
c++·算法·leetcode·动态规划
迷途之人不知返10 小时前
Stack & Queue
c++·算法
(Charon)10 小时前
【C++/Qt】Qt 实现 MQTT 测试工具:连接 Broker、订阅主题与发布消息
开发语言·c++·qt
春蕾夏荷_72829772510 小时前
1、c++ acl udp服务器客户端简单实例-服务器端(1)
服务器·c++·udp