Problem description
Xiaoming doesn't like the number 2 very much, including those that contain the number 2 in the digits. If the digits of a number do not contain the number 2, Xiao Ming calls it a clean number.
How many cleansing numbers are integers 1 to n?
Input format
The first line of input contains an integer n.
Output format The
output line contains an integer, indicating the answer.
Sample input
30
sample output
18
Evaluation use case size and agreement
For 40% of evaluation use cases, 1 <= n <= 10000.
For 80% of the evaluation use cases, 1 <= n <= 100000.
For all evaluation use cases, 1 <= n <= 1000000.
Idea: At first, I wanted to use split to split it out one by one to judge and compare, but it would be very troublesome to write, so I used while. This question is actually a question similar to daffodil, as long as it is 2, it will be re-circulated, and finally ++ That's it.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int ans = 0;
A: for (int i = 1; i <= n; i++) { // A:开始循环
int x = i; // x替换i
while (x != 0) { // x不等于0,进入while循环
int t = x % 10; // 取位
if (t == 2) { // 判断是2
continue A; // 跳转A,继续循环
}
x /= 10; // 取每一位
}
ans++; // 结果
}
System.out.println(ans); // 输出
}
}