算法练习题41---蓝桥杯2021国赛“纯质数”&“完全日期”

前言

蓝桥杯2021年国赛B组,填空题(C++)

一、纯质数

思路

按照题意的要求,就是检查每个数的每一位是否是质数,并且这个数是否是质数,如果这两点都符合,就是纯质数。这里推荐用string库来解决问题。

答案:1903

代码

#include<bits/stdc++.h>
using namespace std;
int ans=1;
bool judge(int n)
{
	for(int i=2;i<=sqrt(n);i++)
	{
		if(n%i==0)
		{
			return false;
		}
	}
	string s=to_string(n);
	if(s.find("1")!=-1||s.find("4")!=-1||s.find("6")!=-1||s.find("8")!=-1||s.find("9")!=-1||s.find("0")!=-1)
	{
		return false;
	}
	return true;
}
int main()
{
	for(int i=3;i<=20210605;i++)
	{
		if(judge(i))
		{
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

二、完全日期

思路

这个题目主要是需要额外考虑闰年对2月份天数的印象,剩下的个人感觉按照题目要求模拟就行,日期类的c++感觉没有太好的办法解决,主要还是模拟整个日期累加的过程

答案:977

代码

#include<bits/stdc++.h>
using namespace std;
int count_sum(int n)
{
	int temp=0;
	while(n!=0)
	{
		temp+=n%10;
		n/=10;
	}
	return temp;
}
int main()
{
	int year=2001;
	int month=1;
	int day=1;
	int sum=0;
	int ans=0;
	while(year<=2021)
	{
		month=1;
		if(year%400==0||(year%4==0&&year%100!=0))  //闰年
		{
			while(month<=12)
			{
				day=1;
				if(month==4||month==6||month==9||month==11)
				{
					while(day<=30)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
				else if(month==2)
				{
					while(day<=29)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
				else
				{
					while(day<=31)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
			}
		}
		else //非闰年
		{
			while(month<=12)
			{
				day=1;
				if(month==4||month==6||month==9||month==11)
				{
					while(day<=30)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
				else if(month==2)
				{
					while(day<=28)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
				else
				{
					while(day<=31)
					{
						sum=0;
						sum=count_sum(year)+count_sum(month)+count_sum(day);
						if((double(sqrt(sum))-int(sqrt(sum)))==0)
						{
							ans++;
						}
						day++;
					}
					month++;
				}
			}
		}
		year++;
	}
	cout<<ans<<endl;
	return 0;
}

感谢Mr_渣渣辉 博主提供的题目集,图片出自2021第十二届蓝桥杯国赛C/C++大学B组题解_Mr_渣渣辉的博客-CSDN博客_2021蓝桥杯c语言b组答案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨大熊的代码世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值