问题描述
小明对数位中含有 2、0、1、9 的数字很感兴趣,
在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。
请问,在 1 到 2019 中,所有这样的数的和是多少?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。
本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
答案:1905111
#include <iostream>
using namespace std;
bool check(int n)
{
while (n)
{
int t = n % 10;
if (t == 2 || t == 0 || t == 1 || t == 9)
{
return true;
}
n /= 10;
}
return false;
}
int main()
{
int ans = 0;
for (int i = 1; i <= 2019; i ++)
{
if (check(i))
{
ans += i;
}
}
cout << ans << endl;
return 0;
}