Last active
January 4, 2020 09:19
-
-
Save qiaoxu123/65a31389f48d2798ca6a282ed1bd9cfe to your computer and use it in GitHub Desktop.
第一种方法:使用字符串来代表数字,实现数值相加进位 第二种方法:使用递归来实现全排列
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
bool Increment(char* number); | |
void PrintNumber(char* number); | |
void Print1ToMaxOfNDigits(int n) { | |
if (n <= 0) | |
return; | |
char* number = new char[n + 1]; | |
memset(number, '0', n); | |
number[n] = '\0'; | |
while (!Increment(number)) { | |
PrintNumber(number); | |
} | |
} | |
//通过进位来判断是否到了最大的n位数 | |
bool Increment(char* number) { | |
bool isOverflow = false; | |
int nTakeOver = 0; | |
int nLength = strlen(number); | |
for (int i = nLength - 1; i >= 0; i--) { | |
int nSum = number[i] - '0' + nTakeOver; | |
if (i == nLength - 1) | |
nSum++; | |
if (nSum >= 10) { | |
if (i == 0) | |
isOverflow = true; | |
else { | |
nSum -= 10; | |
nTakeOver = 1; | |
number[i] = '0' + nSum; | |
} | |
} | |
else { | |
number[i] = '0' + nSum; | |
break; | |
} | |
} | |
return isOverflow; | |
} | |
//打印输出 | |
void PrintNumber(char* number) { | |
bool isBeginning0 = true; | |
int nLength = strlen(number); | |
for (int i = 0; i < nLength; ++i) { | |
if (isBeginning0 && number[i] != '0') { | |
isBeginning0 = false; | |
} | |
if (!isBeginning0) | |
printf("%c", number[i]); | |
} | |
printf("\t"); | |
} | |
int main() { | |
Print1ToMaxOfNDigits(3); | |
getchar(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void PrintNumber(char* number); | |
void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index); | |
void Print1ToMaxOfNDigits(int n) { | |
if (n <= 0) | |
return; | |
char* number = new char[n + 1]; | |
number[n] = '\0'; | |
for (int i = 0; i < 10; ++i) { | |
number[0] = i + '0'; | |
Print1ToMaxOfNDigitsRecursively(number, n, 0); | |
} | |
delete[] number; | |
} | |
void Print1ToMaxOfNDigitsRecursively(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'; | |
Print1ToMaxOfNDigitsRecursively(number, length, index + 1); | |
} | |
} | |
//打印输出 | |
void PrintNumber(char* number) { | |
bool isBeginning0 = true; | |
int nLength = strlen(number); | |
for (int i = 0; i < nLength; ++i) { | |
if (isBeginning0 && number[i] != '0') { | |
isBeginning0 = false; | |
} | |
if (!isBeginning0) | |
printf("%c", number[i]); | |
} | |
printf("\t"); | |
} | |
int main() { | |
Print1ToMaxOfNDigits(3); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment