蓝桥杯2020A组c++初赛

门牌制作

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int sum=0;
	for(int i=1;i<=2020;i++)
	{
		string ss=to_string(i);
		int num=count(ss.begin(),ss.end(),'2');
		sum+=num;
	}
	cout<<sum;
}

 既约分数

#include<bits/stdc++.h>
using namespace std;

//最大公约数 
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
	int cnt=0;
	for(int i=1;i<=2020;i++)
	{
		for(int j=1;j<=2020;j++)
		{
			if(gcd(i,j)==1)cnt++;
		}
	}	
	cout<<cnt;
}

 蛇形填数

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int sum=0;
	for(int i=1;i<=38;i++)
	{
		sum+=i;
	}
	//在第39行,前38行有741个数 
//	cout<<sum;
	//第39行有三十九个数,38/2+1=20=741+20
	cout<<761; 
}

 七段码

 

#include<bits/stdc++.h>
using namespace std;

//发光的二极管必须要连成一片才能表示一个字符
//将只点一条灯的状态放入队列里面,每次取队头扩展
//设置一个set储存不重复的合法状态
//7个二极管,7位二进制数表示
const int N=7,M=1<<N;
bool g[N][N];//两个字母能连通 

int main()
{
	//a b c d e f g
	//0 1 2 3 4 5 6
	g[0][1]=g[0][5]=1;
	g[1][0]=g[1][6]=g[1][2]=1;
	g[2][3]=g[2][6]=g[2][1]=1;
	g[3][2]=g[3][4]=1;
	g[4][3]=g[4][5]=g[4][6]=1;
	g[5][0]=g[5][4]=g[5][6]=1;
	g[6][1]=g[6][2]=g[6][4]=g[6][5]=1;
	int cnt=0;
	//用队列依次扩展状态 
	//将可行的状态放到set里面去重
	queue<int>q;
	set<int>state;
	for(int i=0;i<7;i++)
	{
		//刚开始只亮一条灯 
		q.push(1<<i);
		state.insert(1<<i);
	 } 
	 while(!q.empty())
	 {
	 	//取队头元素扩展 
	 	int t=q.front();
	 	q.pop();
	 	//枚举第几位是否已经亮灯 
	 	for(int i=0;i<7;i++)
	 	{
	 		//该位亮灯,扩展与这条灯相邻的灯,形成新状态 
	 		if((t>>i)&1)
	 		{
	 			for(int j=0;j<7;j++)
	 			{
	 				//灯是否相邻?且该灯还没亮过 
	 				if(g[i][j]&&((t>>j)&1)==0)
					 {
					 	int newst=t+(1<<j);
					 	if(state.find(newst)==state.end())
					 	{
					 		state.insert(newst);
					 		q.push(newst);
						 }
					}	
				} 
			 }
		 }
	 	
	 }
	 cout<<state.size()<<endl;
//	 for(auto c:state)cout<<c<<" ";
 } 

成绩分析

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int sum=0,maxv=0,minv=100;
	for(int i=0;i<n;i++)
	{
		int sc;
		cin>>sc;
		if(sc>maxv)maxv=sc;
		if(sc<minv)minv=sc;
		sum+=sc;
	}
	cout<<maxv<<endl<<minv<<endl;
	double aver=sum*1.0/n;
	printf("%.2lf",aver);
}

回文日期

#include<bits/stdc++.h>
using namespace std;
//判断是否是闰年 
bool run(int year)
{
	if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )return true;
	else return false; 
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		string date;
		cin>>date;
		bool fg1=0,fg2=0;
		int year=stoi(date.substr(0,4));
		int nday=stoi(date.substr(4));

		//从当前年份日期开始寻找 
		for(int i=year;i<=9999;i++)
		{
			string md=to_string(i);
			reverse(md.begin(),md.end());
			//判断月份是否合法
			int mon=(md[0]-'0')*10+(md[1]-'0');
			int day=(md[2]-'0')*10+(md[3]-'0');
			if(i==year&&nday>=mon*100+day)continue;
			if(mon<1||mon>12||day>31||day<1)continue;
			//1,3,5,7,8,10,12-->31天
			//4,6,9,11-->30
			//2月有28天
			if(mon==2&&run(i)&&day>29)continue;
			if(mon==2&&!run(i)&&day>28)continue;
			if((mon==4||mon==6||mon==9||mon==11)&&day>=31)continue;
			
			//找到下一个回文日期
			if(!fg1)
			{
				string next1=to_string(i)+md;
				cout<<next1<<endl;
				fg1=1;
			}
			if(!fg2&&mon==day)
			{
				string next2=to_string(i)+md;
				cout<<next2<<endl;
				fg2=1;
			}
			if(fg1&&fg2)break;
		}
	}
}

子串分值

 O(n^2)会超时,过六个点

#include<bits/stdc++.h>
using namespace std;

//枚举不同的区间起点0-n-1
//枚举不同的区间终点
//对一个子串,如果再新加一个字母
//1.该字母已经出现过,则新子串的f比原来小1
//2.该字母没出现过,则新子串的f比原来大1
const int N=1e5+10;
int st[27];
string s;
int f[N];
 
int main()
{
	cin>>s;
	int len=s.size();
	int sum=0;
	//枚举起点从哪开始 
	for(int i=0;i<len;i++)
	{
		//f存储i为起点,j为终点的子串中恰好出现一次字符的个数 
		for(int j=0;j<=len;j++)
			f[j]=0;
		memset(st,0,sizeof st);
		//起点设置为已经用过
		int cid=s[i]-'a';
		st[cid]=1; 
		f[i]=1;
		sum+=f[i];
		for(int j=i+1;j<len;j++)
		{
			int cid=s[j]-'a';//当前添加到子串的字母对应的下标号;
			if(!st[cid]) 
			{
				//当前字母没出现过
				f[j]= f[j-1]+1;
			}
			else if(st[cid]==1) 
			{
				//当前字母出现过一次
				f[j]=f[j-1]-1; 
			}
			else
			{
			    //当前字母出现过不止一次
			    f[j]=f[j-1];
			}
			sum+=f[j];
// 			cout<<f[j]<<' ';
			st[cid]++;
		}
// 		cout<<endl;
	}
	cout<<sum;
	return 0;
 } 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值