首页 > 其他 > 详细

<剑指offer> 第9题

时间:2019-08-09 10:15:52      阅读:84      评论:0      收藏:0      [点我收藏+]

题目:

输入数字n,按顺序打印出从1到n位最大十进制数的数值。比如输入3,则打印出1、2、3一直到最大三位数999

public class Ninth {
    public static int[] getMax(int n){
        if(n <= 0 ){
            return null;
        }
        if(n == 1){
            int[] res = new int[10];
            for(int i = 1; i < 10; i ++){
                res[i] = i;
            }
            return res;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(‘1‘);
        for(int i = 0; i < n ; i ++){
            sb.append(‘0‘);
        }
        int max = Integer.parseInt(sb.toString());
        int[] res = new int[max];
        for(int i = 1; i < max; i ++){
            res[i] = i;
        }
        return res;

    }

    public static void main(String[] args){
        int[] arr = getMax(4);
        for(int i : arr){
            System.out.println(i);
        }
    }
}

 

<剑指offer> 第9题

原文:https://www.cnblogs.com/HarSong13/p/11325345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!