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)
cout<<number[i];
}
cout<<"\t";
}
void print1ToMax(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;
}
void printTomaxRecursively(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';
printTomaxRecursively(number,length,index+1);
}
}
void print2ToMax(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';
printTomaxRecursively(number,n,0);
}
delete[] number;
}
转载于:https://www.cnblogs.com/XiaoXiaMi123/p/8660031.html