P8680 [蓝桥杯 2019 省 B] 特别数的和 题解

题目传送门

思路与分析

这是一道简单的枚举题,从 \(1\)\(n\) 依次将数位拆开判断即可。

代码

#include<bits/stdc++.h>
using namespace std;
bool check(int x){//判断函数
	while(x){
		if(x%10==2||x%10==0||x%10==1||x%10==9){//是否出现2,0,1,9
			return true;//如果出现,符合要求
		}
		x/=10;//拆掉一位
	}
	return false;//不符合
}
int n,sum;//sum为求和变量
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;i++){
		if(check(i)){//如果为真
			sum+=i;//就累加
		}
	}
	cout<<sum<<endl;//输出和
	return 0;
} 
posted @ 2023-07-09 19:35  扶桃o  阅读(32)  评论(0)    收藏  举报