AcWing 1245. 特别数的和 (枚举)

这个题就比较简单了,也是去年B组的一题。

添加链接描述

循环一边,依次判断。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String []args) throws IOException {
        int n = Integer.parseInt(br.readLine());
        long res = 0;
        for (int i = 1; i <= n; i++)
            if (check(i))
                res += i;
        pw.print(res);
        pw.flush();
        pw.close();
        br.close();
    }

    private static boolean check(int u) {
        String temp = String.valueOf(u);
        for (int i = 0; i < temp.length(); i++)
            if (temp.charAt(i) == '2' || temp.charAt(i) == '0' || temp.charAt(i) == '1' || temp.charAt(i) == '9')
                return true;
        return false;
    }
}
发布了167 篇原创文章 · 获赞 3 · 访问量 3427

猜你喜欢

转载自blog.csdn.net/qq_43515011/article/details/104288387