zoukankan      html  css  js  c++  java
  • 面试题12:打印1到最大的n位数

    题目:

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

    题目的陷阱:

      当输入的n很大时,此时就是一个大数问题。

    解决方案:

      在数组或者字符串上模拟数字加法的解法,绕过陷阱

    Java代码:

     1 package com.hb;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 public class Print1ToMaxOfNDigits {
     7     public void Print1ToMaxofNDigits(int n){
     8         //用nlist表示数n,nlist[0]表示n的最高位 ------注意,看别人程序时,有可能有的地方不是对的,还是要有自己的思想
     9         ArrayList<Integer> nlist = new ArrayList<Integer>();
    10         for(int i = 0 ; i < n ; i++){
    11             nlist.add(0);  //将元素添加到列表的尾部
    12         }
    13     
    14         increment(nlist);    
    15     }
    16         
    17     //用数组模拟数字+1
    18     public static void increment(ArrayList<Integer> nlist){
    19         int carrybit = 0 ;   //carrybit是进位的意思
    20         boolean end = false ;
    21         while(true){
    22             
    23             for(int i = nlist.size() - 1 ;  i >= 0 ; i--){
    24                 int digit = nlist.get(i);
    25                 int sum = digit + carrybit ;    //从个位开始,加1
    26                 if(i == (nlist.size() - 1)){
    27                     sum += 1 ;  //sum++
    28                 }
    29                 if(sum >= 10){
    30                     //最高位产生进位,达到最大值,停止输出
    31                     if(i == 0 ){
    32                         end = true;
    33                     }
    34                     sum = sum - 10;
    35                     carrybit = 1 ;
    36                 }else{
    37                     carrybit = 0 ;
    38                 }
    39                 
    40                 nlist.set(i, sum);    
    41             }  //for循环结束        
    42             
    43             output(nlist);            
    44             if(end) {break;    }            
    45         }
    46     }
    47         
    48     //输出数字,将高位的 0 舍掉
    49     public static void output(ArrayList<Integer>  nlist){
    50         Iterator<Integer> ite = nlist.iterator();
    51         int num;
    52         //找到第一个为 0 的位置
    53         boolean first = false ;
    54         
    55         while (ite.hasNext()){
    56             if(first){
    57                 System.out.print("--" + ite.next());
    58                 continue;
    59             }
    60             if((num = ite.next()) != 0){
    61                 first = true ;
    62                 System.out.print("+++"+num);
    63             }
    64         }        
    65         System.out.println();        
    66     }
    67     
    68     public static void main(String[] args) {
    69         Print1ToMaxOfNDigits  p = new Print1ToMaxOfNDigits();
    70         int n = 2;
    71         p.Print1ToMaxofNDigits(n);
    72     }
    73     
    74     
    75 
    76 }
    View Code

     转载:http://www.2cto.com/kf/201311/256909.html

      

  • 相关阅读:
    Leetcode 191.位1的个数 By Python
    反向传播的推导
    Leetcode 268.缺失数字 By Python
    Leetcode 326.3的幂 By Python
    Leetcode 28.实现strStr() By Python
    Leetcode 7.反转整数 By Python
    Leetcode 125.验证回文串 By Python
    Leetcode 1.两数之和 By Python
    Hdoj 1008.Elevator 题解
    TZOJ 车辆拥挤相互往里走
  • 原文地址:https://www.cnblogs.com/Mokaffe/p/4337134.html
Copyright © 2011-2022 走看看