这种情况需要明确是否考虑大数。
个人感觉实现起来 用加1操作比较简单,但是考虑起来还需要细心
主要就是从低位开始每一次加1 然后打印。判断边界条件需要明确一个原则,最高位如果有进位 那么则超过范围,不打印
#include "stdio.h"
int incre(char *number)
{
int length = strlen(number);
int takeover =0;
int index;
int sum=0;
int ret =0;
for(index=length-1;index>=0;index--)
{
sum = number[index]-'0'+takeover;
if(index==length-1)
sum++;
if(sum>9)
{
if(index==0)
{
ret =1;
}
number[index]=sum%10+'0';
takeover = sum/10;
}
else
{
takeover =0;
number[index]=sum+'0';
}
}
return ret;
}
void printNum(char *number)
{
int i=0;
if(number ==NULL)
return;
for(i=0;number[i]=='0';i++)
{}
printf("%s \n",(number+i));
}
int main()
{
int i;
int nLength;
char *number;
scanf("%d",&nLength);
number = (char *)malloc(sizeof(char)*(nLength+1));
memset(number,'0',nLength);
number[nLength]='\0';
printf("%d",strlen(number));
while(!incre(number))
{
printNum(number);
}
return 0;
}
海涛的递归算法昨天看了,今天上午可就是没想出如何写,再思考一下
思路就是 先通过第1位 0-9不停的遍历相互
类似于10个十叉树吧,这样每个分支用回溯发遍历。这样写代码 ,感觉不舒服
#include "stdio.h"
void printNum(char *number)
{
int i=0;
if(number ==NULL)
return;
for(i=0;number[i]=='0';i++)
{}
printf("%s \n",(number+i));
}
void traverseNumLate(char *number,int nLength,int index)
{
int digitIndex =0;
int maxDigit = 10;
if(index==nLength-1)
{
printNum(number);
return;
}
for(digitIndex=0;digitIndex<10;digitIndex++)
{
number[index+1]=digitIndex+'0';
traverseNumLate(number,nLength,index+1);
}
}
void traverseNum(char *number,int nLength)
{
int digitIndex =0;
int maxDigit = 10;
for(digitIndex=0;digitIndex<10;digitIndex++)
{
number[0]=digitIndex+'0';
traverseNumLate(number,nLength,0);
}
}
int main()
{
int i;
int nLength;
char *number;
scanf("%d",&nLength);
number = (char *)malloc(sizeof(char)*(nLength+1));
memset(number,'0',nLength);
number[nLength]='\0';
traverseNum(number,nLength);
free(number);
}