topic
[Problem description]
Xiao Ming dislikes 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 clean numbers are there among the integers 1 to n?
[Input format]
The first line of input contains an integer n.
[Output format] The
output line contains an integer to indicate the answer.
[Sample input]
30
[Sample output]
18
[Evaluation use case scale and conventions]
For 40% of evaluation use cases, 1 <= n <= 10000.
For 80% of the test cases, 1 <= n <= 100000.
For all measurement cases, 1 <= n <= 1000000.
Problem solving ideas
Convert a number of type int to a string of type String, and then use the contains method of the string to determine whether the string contains "2". If it does, it is not a clean number, and vice versa, count++.
hint
The contains method of a string determines whether it contains a string , and the double quotes cannot be dropped
Code
import java.util.Scanner;
public class Main {
//蓝桥杯要求class命名为Main,且无package
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int count = 0;
for(int i=1;i<=n;i++){
String str = i+"";//转为字符串
if(str.contains("2")){
continue;//不为洁净数continue结束当前for循环
}
count++;//为洁净数则count++
}
System.out.println(count);
}
}