天天看點

程式設計題:列印1到最大的n位數

列印1到最大的n位數:

這道題簡單也不簡單,考慮如果n位數小,在int,long範圍内;如果n比較大,超出long範圍是一個大數。

思路:

數字較小情況:

方法1.在int,long範圍内,可以直接根據n來獲得最大位數,然後直接一個for循環輸出。

一個大數問題,要用字元串或者數組來實作:在大數的加法中,我們需要注意的問題是進位問題。在該題中還需要判斷是否為最大的n位整數。這裡需要一個小技巧,我們隻需判斷最高位(第n位)是否要進位,如果需進位,則已經為最大數。

方法2.用數組實作。

方法3.用全排列思想實作。

代碼:

package com.mmall;

import com.mmall.model.User;

import java.util.Scanner;

/**
 * Created by ASUS on 2018/7/13
 *
 * @Authod Grey Wolf
 */
public class Test1 {

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        test1.sys1();
        System.out.println();
        test1.sys2();
        System.out.println();
        test1.sys3();
    }

    /**
     * 方法3
     */
    private void sys3() {
        Scanner scan=new Scanner(System.in);
        System.out.println("方法3--》輸入整數的位數:");
        int n=scan.nextInt();
        if (n<=0){
            return;
        }
        int []number=new int[n];
        int i;
        for(i=0;i<10;i++){
            number[0]=i;
            printMaxOfNdigits(number,n,0);
        }
    }

    /**
     * 全排列思想
     * @param number
     * @param length
     * @param index
     */
    private void printMaxOfNdigits(int[] number, int length, int index) {
        if (index==length-1){
            printNumber(number);
            return;
        }
        int i;
        for (i=0;i<10;i++){
            number[index+1]=i;
            printMaxOfNdigits(number,length,index+1);
        }
    }

    /**
     * 方法2
     */
    private void sys2() {
        Scanner scan=new Scanner(System.in);
        System.out.println("方法2--》輸入整數的位數:");
        int n=scan.nextInt();
        int [] number=new int[n];
        while (!increment(number)){
            printNumber(number);
        }
    }

    /**
     *判斷是否達到最大n位數,解決進位問題
     * @param number
     * @return
     */
    public boolean increment(int [] number){
        boolean isOverflow=false;
        int nTakeOver=0;
        int i;
        for (i=number.length-1;i>=0;i--){
            int nSum=number[i]+nTakeOver;
            if (i==number.length-1){
                nSum++;
            }
            if (nSum>=10){
                if (i==0){
                    isOverflow=true;
                }else {
                    nTakeOver=1;
                    nSum-=10;
                    number[i]=nSum;
                }
            }else {
                number[i]=nSum;
                break;
            }
        }
        return isOverflow;
    }


    /**
     * 方法1
     */
    private void sys1() {
        Scanner scan = new Scanner(System.in);
        System.out.println("方法1--》輸入整數的位數:");
          int time=scan.nextInt();
          int i;
          int num=1;
          for(i=1;i<=time;i++){
              num*=10;
          }
          for (i=1;i<num;i++){
              System.out.print(i+" ");
          }


    }

    /**
     * 輸出數字
     * @param number
     */
    public void printNumber(int []number){
        boolean isBeginning=true;
        int i;
        for (i=0;i<number.length;i++){
            if (isBeginning&&number[i]!=0){
                isBeginning=false;
            }
            if (!isBeginning) {
                System.out.print(number[i]);
            }
        }
        System.out.print(" ");
    }



}
      

效果:

方法1--》輸入整數的位數:

2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

方法2--》輸入整數的位數:

2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

方法3--》輸入整數的位數:

2

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

Process finished with exit code 0

我的座右銘:不會,我可以學;落後,我可以追趕;跌倒,我可以站起來;我一定行。

繼續閱讀