Acwing---1245.特别数的和

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

特别数的和

1.题目

小明对数位中含有 2、0、1、9 的数字很感兴趣不包括前导 0在 1 到 40 中这样的数包括 1、2、9、10 至

32、39 和 40共 28 个他们的和是 574。

请问在 1 到 n 中所有这样的数的和是多少

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

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

数据范围
1 ≤ n ≤ 10000 1≤n≤10000 1n10000

输入样例

40

输出样例

574

2.基本思想

暴力枚举即可

Tips:.取出x的每位数字

int t = x % 10;
x /= 10;

3.代码实现

import java.util.Scanner;

public class _1245特别数的和 {
    static Scanner sc = new Scanner(System.in);
    static int res = 0;

    public static void main(String[] args) {
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++)
            if (check(i)) res += i;
        //if (check1(i)) res += i;

        System.out.println(res);
    }

    static boolean check(int num) {
        String s = String.valueOf(num);
        if (s.contains("2")) return true;
        if (s.contains("0")) return true;
        if (s.contains("1")) return true;
        if (s.contains("9")) return true;
        return false;
    }

    static boolean check1(int num) {
        while (num > 0) {
            int t = num % 10;
            num = num / 10;
            if (t == 2 || t == 0 || t == 1 || t == 9) {
                return true;
            }
        }
        return false;
    }
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6