Lanqiao Cup: From 1 to 2019, how many digits contain the number 9? (Number nine) (Question-solving ideas) (answer)

topic

[Problem description]
From 1 to 2019, how many digits contain the number 9?
Note that some numbers contain multiple 9s in the digits, and this number is only counted once. For example, the number 1999 contains the number 9, which is just a number in the calculation.
[Answer Submission]
This is a question that fills in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer. If you fill in the extra content, you will not be able to score.

answer

  544

Problem solving ideas

  Convert the number to a string, if the string contains "9", then count++

Code

public class Main {
    
     //蓝桥杯要求class命名为Main,且无package
    public static void main(String []args){
    
    
        int count=0;
        for(int i=1;i<=2019;i++){
    
    
            String str = i+""; //将数字转为字符串
            if(str.contains("9")){
    
     //如果字符串中包含9,count++
                count++;
            }
        }
        System.out.println(count);
    }
}

Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108908523