JZ-C-12

剑指offer第十二题:打印1到最大的n个数,"大数问题"

  1 //============================================================================
  2 // Name        : JZ-C-12.cpp
  3 // Author      : Laughing_Lz
  4 // Version     :
  5 // Copyright   : All Right Reserved
  6 // Description : 剑指offer第十二题:打印1到最大的n个数,"大数问题"
  7 //============================================================================
  8 
  9 #include <iostream>
 10 #include <string.h>
 11 #include  <memory>//memory是C++空间配置器以及new delete定义的头文件,里面定义了空间配置器,new delete以及一些用于调用构造函数的函数。
 12 using namespace std;
 13 
 14 void PrintNumber(char* number);
 15 bool Increment(char* number);
 16 void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index);
 17 
 18 void Print1ToMaxOfNDigits_1(int n) {
 19     if (n <= 0)
 20         return;
 21 
 22     char *number = new char[n + 1];
 23     memset(number, '0', n);
 24     number[n] = '\0';
 25 
 26     while (!Increment(number)) {
 27         PrintNumber(number);
 28     }
 29 
 30     delete[] number;
 31 }
 32 // 字符串number表示一个数字,在 number上增加1
 33 // 如果做加法溢出,则返回true;否则为false
 34 bool Increment(char* number) {
 35     bool isOverflow = false;
 36     int nTakeOver = 0;
 37     int nLength = strlen(number);
 38 
 39     for (int i = nLength - 1; i >= 0; i--) {
 40         int nSum = number[i] - '0' + nTakeOver;
 41         if (i == nLength - 1)
 42             nSum++;
 43 
 44         if (nSum >= 10) {
 45             if (i == 0)
 46                 isOverflow = true;
 47             else {
 48                 nSum -= 10;
 49                 nTakeOver = 1;
 50                 number[i] = '0' + nSum;
 51             }
 52         } else {
 53             number[i] = '0' + nSum;
 54             break;
 55         }
 56     }
 57 
 58     return isOverflow;
 59 }
 60 // ====================方法二====================
 61 void Print1ToMaxOfNDigits_2(int n) {
 62     if (n <= 0)
 63         return;
 64 
 65     char* number = new char[n + 1];
 66     number[n] = '\0';
 67 
 68     for (int i = 0; i < 10; ++i) {
 69         number[0] = i + '0';
 70         Print1ToMaxOfNDigitsRecursively(number, n, 0);
 71     }
 72 
 73     delete[] number;
 74 }
 75 
 76 void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index) {
 77     if (index == length - 1) {
 78         PrintNumber(number);//打印,结束一次循环
 79         return;
 80     }
 81 
 82     for (int i = 0; i < 10; ++i) {
 83         number[index + 1] = i + '0';//可以将整数转成字符
 84         Print1ToMaxOfNDigitsRecursively(number, length, index + 1);
 85     }
 86 }
 87 
 88 // ====================公共函数====================
 89 // 字符串number表示一个数字,数字有若干个0开头
 90 // 打印出这个数字,并忽略开头的0
 91 void PrintNumber(char* number) {
 92     bool isBeginning0 = true;
 93     int nLength = strlen(number);
 94 
 95     for (int i = 0; i < nLength; ++i) {
 96         if (isBeginning0 && number[i] != '0')
 97             isBeginning0 = false;
 98 
 99         if (!isBeginning0) {
100 //            printf("%c", number[i]);
101             cout << number[i];
102         }
103     }
104 
105 //printf("\t");
106     cout << " ";
107 }
108 
109 // ====================测试代码====================
110 void Test(int n) {
111 //printf("Test for %d begins:\n", n);
112     cout << "Test for " << n << " begins:" << endl;
113     Print1ToMaxOfNDigits_1(n);
114 //    Print1ToMaxOfNDigits_2(n);
115 
116 //printf("Test for %d ends.\n", n);
117     cout << endl << "Test for " << n << " ends." << endl;
118 }
119 
120 int main(int argc, char **argv) {
121     Test(1);
122     Test(2);
123     Test(3);
124     Test(0);
125     Test(-1);
126     return 0;
127 }

 

posted @ 2016-06-01 20:48  回看欧洲  阅读(185)  评论(0编辑  收藏  举报