【蓝桥杯java】特别数的和

题目描述

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

样例

【输入格式】
输入一行包含两个整数 n。
【输出格式】
输出一行,包含一个整数,表示满足条件的数的和。
【样例输入】
40
【样例输出】
574

java

public class Main {

	static Boolean check(int t) {
		
		while(t>0) {
			if(t%10==0 || t%10==2 || t%10==1 || t%10==9) {
				return true;
			}
			t/=10;
		}
		return false;
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader input =new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(input.readLine());
		int sum=0;
		for (int i = 1; i <=n; i++) {
			//看看i是否有1,2,9,10这几个数字
			int t=i;
			if(check(t)) {
				sum+=i;
			}
		}
		
		System.out.println(sum);
		
	}

}

猜你喜欢

转载自blog.csdn.net/Black_Customer/article/details/108955873