这里需要注意数字范围的问题,如果是n位数字可能超过C语言中数字的表示范围,所以采用n位数字的全排列方式进行解决,或者采用字符串表示整数的方法。
#include "stdafx.h"
#include <iostream>
using namespace std;
void rshow(char* buf,int len,int index)
{
if(index==len)
{
cout<<buf<<endl;
return;
}
for(int i=0;i<=9;i++)
{
buf[index]='0'+i;
rshow(buf,len,index+1);
}
return;
}
int main(void)
{
int n=3;
char* buf=(char*)malloc((n+1)*sizeof(char));
buf[n]='\0';
rshow(buf,n,0);
system("pause");
return 0;
}