标签:
题目描述:9
分析:
这是一个大数问题,利用字符串模拟运算。
#include<cstdio> #include<cstring> #include<cstdlib> 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("\n"); } 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); } delete []number; } int main() { int n; while(scanf("%d",&n) != EOF) { Print1ToMaxOfNDigits(n); } return 0; }
#include<cstdio> #include<cstring> #include<cstdlib> 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("\n"); } 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 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; } int main() { int n; while(scanf("%d",&n) != EOF) { Print1ToMaxOfNDigits(n); } return 0; }
标签:
原文地址:http://blog.csdn.net/biluyang/article/details/44063553