注意:需考虑大数问题
void PrintFrom1ToMax(int n) { if (n <= 0) return; char* arrPrintStr = new char[n+1]; memset(arrPrintStr, ‘0‘, n); arrPrintStr[n] = ‘\0‘; while (!Increment(arrPrintStr)) { PrintNumber(arrPrintStr); } delete arrPrintStr[]; } bool Increment(char* arrNumber) { bool bIsOverflow = false; int nTakeOver = 0; int nLength = strlen(arrNumber); for (int i = nLength - 1; i >= 0; i--) { int num = arrNumber[i] - ‘0‘ + nTakeOver; if (i == nLength - 1) num++; if (num >= 10) { if (i == 0) bIsOverflow = true; else { num -= 10; nTakeOver = 1; arrNumber[i] = ‘0‘ + num; } } else { arrNumber[i] = ‘0‘ + num; break; } } return bIsOverflow; } void PrintNumber(char* arrNumber) { bool bStartPrint = false; int nLength = strlen(arrNumber); for (int i = 0; i < nLength; ++i) { if (!bStartPrint && arrNumber[i] != ‘0‘) bStartPrint = true; if (bStartPrint) printf("%c", arrNumber[i]); } }
原文:https://www.cnblogs.com/yapp/p/14389053.html