特别数的和

特别数的和

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

输入格式
共一行,包含一个整数 n。

输出格式
共一行,包含一个整数,表示满足条件的数的和。

数据范围
1≤n≤10000
输入样例:

40

输出样例:

574

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

typedef long long LL;

int n;
LL res=0;
bool judge(int t){
    
    
    int s[10];
    memset(s,0,sizeof(s));
    while(t>=10){
    
    
        s[t%10]++;
        t/=10;
    }
    s[t]++;
    if(s[1]||s[0]||s[2]||s[9])
        return true;
    else
        return false;
}

int main(){
    
    
    cin >> n;
    for(int i=1;i<=n;i++){
    
    
        if(judge(i))
            res+=(LL)i;
    }
    cout << res;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhongxinyun/article/details/113816325