多多的数字组合(Java)

https://www.nowcoder.com/exam/test/82584972/detail?pid=30579184

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public int method(){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        if(N<=0 || N>45){
            return -1;
        }

        String out_str = "";
        int max_num = 9;
        while(N>0){
            if(N>max_num){
                N = N-max_num;
                out_str = max_num+out_str;
                max_num--;
            }else {
                out_str = N+out_str;
                break;
            }
        }
        int out = Integer.parseInt(out_str);
        return out;
    }
    public static void main(String[] args) {
        Main m = new Main();
        System.out.println(m.method());
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务