【蓝桥杯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);
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值