【蓝桥杯】十一届蓝桥杯软件类第二场C/C++大学B组

A:门牌制作

题目描述

在这里插入图片描述

题解

本题关键在于把int类型转换为string类型,然后判断所有string字符串中字符 ‘2’ 的个数。
c++11标准增加了全局函数std::to_string
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

代码

解法1,不用转换数据类型

#include<cstdio>
#include<iostream>
using namespace std;

int check(int i)
{
	int res = 0;
	while(i>0){
		int k=i%10;
		if(k==2) res++;
		i/=10;
	}
	return res; 
}

int main()
{
	int count = 0;
	for(int i=1;i<=2020;i++){
		count+=check(i);
	}
	printf("%d",count);
	return 0;	
} 

解法2

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int count =0;
	for(int i=1;i<=2020;i++)
	{
		string mid = to_string(i);
		for(int j=0;j<mid.size();j++)
		{
			if(mid[j]=='2')
				count++;
		}
	}
	cout<<count;	
	return 0;	
} 

答案为624
在这里插入图片描述


B:既约分数

在这里插入图片描述

题解

直接辗转相除法求最大公约数

代码

#include<cstdio>

using namespace std;
//辗转相除求公约数 
int gcd(int a,int b)
{
	return b==0 ? a : gcd(b,a%b);
}
int main()
{
	int sum=0;
	for(int i=1;i<=2020;i++)
	{
		for(int j=1;j<=2020;j++)
		{
			if(gcd(i,j)==1) sum++;
		}	
	}
	printf("%d",sum); 
	return 0;
}

答案

在这里插入图片描述


C:蛇形填数

在这里插入图片描述

答案

761


D:跑步训练

在这里插入图片描述

答案

8879


E:七段码

在这里插入图片描述
在这里插入图片描述

答案

80


F:成绩统计

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码1,使用 round()函数四舍五入

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	int x;
	int number1=0,number2=0;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		if(x>=60)
		number1++;
		if(x>=85)
		number2++;
	}
	int d1=round(100*1.0*number1/n);
	int d2=round(100*1.0*number2/n);
	cout<<d1<<"%\n"<<d2<<"%\n";
} 

代码2

#include<iostream> 
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	int n = 0;
	scanf("%d",&n);	
	float pass=0;
	float good=0;
	
	int score=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&score);
		if(score>=60)
			pass++;
		if(score>=85)
			good++;
	}
	int i_pass=pass;
	int i_good=good;
	
	float pass_rate=(pass/n)*100;
	int ipass_rate=i_pass*100/n;
	
	float good_rate=(good/n)*100;
	int igood_rate=i_good*100/n;
	
	if(pass_rate>=ipass_rate+0.5) printf("%d%\n",ipass_rate+1);
	else printf("%d%\n",ipass_rate);
	
	if(good_rate>=igood_rate+0.5) printf("%d%\n",igood_rate+1);
	else printf("%d%\n",igood_rate);
	
	return 0;	
} 

G:回文日期

在这里插入图片描述
在这里插入图片描述

题解

首先让输入的日期递增,此时需要判断月份和每月的日期,同时还需要考虑闰年。
日期每次递增之后再判断回文和AB型回文。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <iostream>

using namespace std;
const int INF = 1e9;
const int maxn = 8;
int x;
int str[maxn];
int cnt;

//闰年:能被4整除但不能被100整除,或能被400整除的
int leap_year[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nonleap_year[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void convert(int x){
	cnt = 0;
	while(x){
		str[7-cnt++] = x % 10;
		x /= 10;
	}
}
bool jg_leap_year(int y){
    return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}
int next_day(int x){
	int y = x / 10000;  //取前四位
	int m = x % 10000 / 100; //取最后四位的前两位
	int d = x % 100;  //取最后两位
    if(d++ == (jg_leap_year(y)? leap_year[m - 1] : nonleap_year[m - 1])){
        d = 1;
        if(m++ == 12){
            y++;
            m = 1;
        }
    }
    return y * 10000 + m * 100 + d;
}

//判断是否为回文
bool jg1(int x){
	convert(x);
	for(int i = 0; i <= 3; i++){
		if(str[i] != str[7 - i]) return false;
	}
	return true;
}
//判断是否为ABABBABA型回文
bool jg2(int x){
	//首先判断是否为回文
	if(jg1(x) == false) return false;
	return (str[0] != str[1] && str[0] == str[2] && str[1] == str[3]);
}
int main(){
	cin >> x;
	int y1 = x;

	while(y1 = next_day(y1)){
		if(jg1(y1)){
			cout << y1 << endl;
			break;
		}
	}
	int y2 = x;
	while(y2 = next_day(y2)){
		if(jg2(y2)){
			cout << y2 ;
			break;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值