Java combat 5: Determine the number of increments

Java combat 5: Determine the number of increments

After writing a question from Lanqiao Cup, after reading other people's writing, I think I wrote it stupidly, so I should remember more commonly used functions. I realized this with an array, which feels weird and troublesome.

Original question:
[Problem description]
If any digit of a positive integer is not greater than the adjacent digit on the right, it is called a digit increasing number. For example, 1135 is a digit increasing number, and 1024 is not a digit increasing number.
Given a positive integer n, how many digits are increasing 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]
26
[Evaluation use case scale and conventions]
For 40% of evaluation use cases, 1 <= n <= 1000.
For 80% of the evaluation use cases, 1 <= n <= 100000.
For all measurement cases, 1 <= n <= 1000000.

code show as below:

import java.util.Scanner;
public class blueSelf_5 {
    
    
    public static void main(String[] args) {
    
    
        Scanner reader=new Scanner(System.in);
        int n;
        n=reader.nextInt();
        int t=0;
        for(int p=1;p<=n;p++){
    
    
            String s;
            s= String.valueOf(p);
            int l=s.length();
            int m;int k;int[] a=new int[l];
            m=Integer.parseInt(s);
            k=m;
            //123
            for(int i=1;i<=l;i++){
    
    //1//2//3
                a[i-1]=k%10;//a[0]=3//a[1]=2//a[2]=1
                k = (k - (k % 10))/10;//k=12//k=1//k=0
            }
            judgeIncrease j=new judgeIncrease();
            if(j.ifIncrease(a)){
    
    
                t++;
            }
        }
        System.out.println(t);
    }
}
class judgeIncrease {
    
    
    public boolean ifIncrease(int[] shuzu) {
    
    
        if (shuzu.length == 1) {
    
    
            return true;
        }
        else {
    
    
            for (int i = 1; i <= shuzu.length-1; i++) {
    
    
                if (shuzu[i-1] < shuzu[i]) {
    
    
                    return false;
                }
            }
            return true;
        }
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46020391/article/details/112330298