第十届蓝桥杯——求和

【问题描述】

小明对数位中含有 2、0、1、9 的数字很感兴趣,在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。
请问,在 1 到 2019 中,所有这样的数的和是多少?

【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。


题解:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

bool judge(int num)
{
	while(num)
	{
		int t = num % 10;
		if(t == 2 || t == 0 || t == 1 || t == 9) return true;
		num /= 10;
	}
	return 0;
}
int main()
{
	int ans = 0;
	for (int i = 1; i <= 2019; i ++) if(judge(i)) ans += i;
	cout << ans << endl;
	return 0;
} 

答案:1905111

如果感觉这篇文章对你有帮助的话,不妨点一个赞,十分感谢(✪ω✪)。
printf(“点个赞吧!”);
cout <<“点个赞吧!”;
System.out.println(“点个赞吧!”);
↓↓↓

发布了63 篇原创文章 · 获赞 5 · 访问量 828

猜你喜欢

转载自blog.csdn.net/weixin_46239370/article/details/105432721
今日推荐