/
041_Print1ToMaxOfNDigits.cpp
123 lines (100 loc) · 2.26 KB
/
041_Print1ToMaxOfNDigits.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Coding Interviews: Questions, Analysis & Solutions
// Harry He
#include <stdio.h>
#include <memory>
void PrintNumber(char* number);
bool Increment(char* number, int length);
void Print1ToMaxOfNDigitsCore(char* number, int length, int index);
// ==================== Solution 1====================
void Print1ToMaxOfNDigits_1(int n)
{
if(n <= 0)
return;
char *number = new char[n + 1];
memset(number, '0', n);
number[n] = '\0';
while(!Increment(number, n))
{
PrintNumber(number);
}
delete []number;
}
bool Increment(char* number, int length)
{
bool isOverflow = false;
int carry = 0;
for(int i = length - 1; i >= 0; i --)
{
int sum = number[i] - '0' + carry;
if(i == length - 1)
sum ++;
if(sum >= 10)
{
if(i == 0)
isOverflow = true;
else
{
sum -= 10;
carry = 1;
number[i] = '0' + sum;
}
}
else
{
number[i] = '0' + sum;
break;
}
}
return isOverflow;
}
// ==================== Solution 2 ====================
void Print1ToMaxOfNDigits_2(int n)
{
if(n <= 0)
return;
char* number = new char[n + 1];
number[n] = '\0';
Print1ToMaxOfNDigitsCore(number, n, -1);
delete[] number;
}
void Print1ToMaxOfNDigitsCore(char* number, int length, int index)
{
if(index == length - 1)
{
PrintNumber(number);
return;
}
for(int i = 0; i < 10; ++i)
{
number[index + 1] = i + '0';
Print1ToMaxOfNDigitsCore(number, length, index + 1);
}
}
// ==================== Common Function ====================
void PrintNumber(char* number)
{
char* pChar = number;
while(*pChar == '0')
++pChar;
if(*pChar != '\0')
printf("%s\t", pChar);
}
// ==================== Test Code ====================
void Test(int n)
{
printf("Test for %d begins:\n", n);
Print1ToMaxOfNDigits_1(n);
printf("\n");
Print1ToMaxOfNDigits_2(n);
printf("\n");
printf("Test for %d ends.\n", n);
}
int main(int argc, char* argv[])
{
Test(1);
Test(2);
Test(3);
Test(0);
Test(-1);
return 0;
}