[Lanqiao Cup pre-exam assault] The 10th Lanqiao Cup Provincial C/C++ University Group B Test Questions F Special Number Sum

  小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到
40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。
  请问,在 1 到 n 中,所有这样的数的和是多少?
不提了,不提了,送分题啊。
果然是难题不一定在后面!!!
#include<iostream>
using namespace std;
int ans;
bool judge(int x){
    
    
	while(x){
    
    
		int y=x%10;
		if(y==2||y==0||y==1||y==9){
    
    
			return true;
		}
		x/=10;
	}
	return false;
}
int main(){
    
    
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
    
    
		if(judge(i)){
    
    
			ans+=i;
		}
	}
	cout<<ans<<endl;
	return 0;
} 

有问题可以留言交流٩(๑❛ᴗ❛๑)۶

Guess you like

Origin blog.csdn.net/kieson_uabc/article/details/109010603
Recommended