zoukankan      html  css  js  c++  java
  • 打印从1到最大的n位数.

    输入数字n,按顺序从打印出从1到最大的n位十进制数.比如输入3,也就是最大的3位数是999,那么就打印1.2.3.---999;

    方法1:非递归

     1 #include "stdafx.h"
     2 #include <iostream>
     3 #include <exception>
     4 using namespace std;
     5 void PrintNumber(char* number)
     6 {
     7     bool isBeginning0 = true;
     8     int nLength = strlen(number);
     9     for(int i = 0;i<nLength;++i)
    10     {
    11         if(isBeginning0&&number[i]!='0')
    12             isBeginning0 = false;
    13         if(!isBeginning0)
    14         {
    15             printf("%c",number[i]);
    16         }
    17 
    18     }
    19     printf("	");
    20 }
    21 bool Increment(char* number)
    22 {
    23     bool isOverflow = false;
    24     int nTakeOver = 0;
    25     int nLength = strlen(number);
    26     for(int i = nLength-1;i>=0;i--){
    27         int nSum = number[i]-'0'+nTakeOver;
    28         if(i==nLength-1)
    29             nSum++;
    30         if(nSum>=10)
    31         {
    32             if(i==0)
    33                 isOverflow=true;
    34             else
    35             {
    36                 nSum -= 10;
    37                 nTakeOver=1;
    38                 number[i]='0'+nSum;
    39 
    40             }
    41         }
    42         else{
    43             number[i]='0'+nSum;
    44             break;
    45         }
    46     }
    47     return isOverflow;
    48 
    49 }
    50 void PrintToMaxOfNDigits(int n)
    51 {
    52     if(n<=0)
    53         return;
    54     char *number = new char[n+1];
    55     memset(number,'0',n);
    56     number[n]='';
    57     while(!Increment(number))
    58     {
    59         PrintNumber(number);
    60     }
    61     delete []number;
    62 }
    63 int _tmain(int argc, _TCHAR* argv[])
    64 {
    65     int n = 4;
    66 PrintToMaxOfNDigits(n);
    67     return 0;
    68 }

     方法2.递归法,全排列用递归表达,数字的每一位都可能是0-9中的一个数,然后设置下一位。递归结束的条件是我们已经设置了数字的最后一位.

     1 #include "stdafx.h"
     2 #include <iostream>
     3 #include <exception>
     4 using namespace std;
     5 void PrintNumber(char* number)
     6 {
     7     bool isBeginning0 = true;
     8     int nLength = strlen(number);
     9     for(int i = 0;i<nLength;++i)
    10     {
    11         if(isBeginning0&&number[i]!='0')
    12             isBeginning0 = false;
    13         if(!isBeginning0)
    14         {
    15             printf("%c",number[i]);
    16         }
    17 
    18     }
    19     printf("	");
    20 }
    21 void Print1ToMaxOfNDigitsRecursively(char* number,int length,int index)
    22 {
    23     if(index==length-1)
    24     {
    25         PrintNumber(number);
    26         return;
    27     }
    28     for(int i = 0;i<10;++i)
    29     {
    30         number[index+1]=i+'0';
    31         Print1ToMaxOfNDigitsRecursively(number,length,index+1);
    32     }
    33 }
    34 void Print1ToMaxOfNDigits(int n)
    35 {
    36     if(n<=0)
    37         return;
    38     char* number = new char[n+1];
    39     number[n]='';
    40     for(int i = 0;i<10;++i)
    41     {
    42         number[0]=i+'0';
    43         Print1ToMaxOfNDigitsRecursively(number,n,0);
    44     }
    45     delete[] number;
    46 
    47 }
    48 int _tmain(int argc, _TCHAR* argv[])
    49 {
    50     int n = 4;
    51 Print1ToMaxOfNDigits(n);
    52     return 0;
    53 }
  • 相关阅读:
    RE
    【LeetCode】198. House Robber
    【LeetCode】053. Maximum Subarray
    【LeetCode】152. Maximum Product Subarray
    【LeetCode】238.Product of Array Except Self
    【LeetCode】042 Trapping Rain Water
    【LeetCode】011 Container With Most Water
    【LeetCode】004. Median of Two Sorted Arrays
    【LeetCode】454 4Sum II
    【LeetCode】259 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/crazycodehzp/p/3527770.html
Copyright © 2011-2022 走看看