C语言函数递归调用的问题

来源:百度知道 编辑:UC知道 时间:2024/07/07 01:01:09
现在有一些字符串,第1个是A
第2个是AB
第3个是ABA
第4个是ABAAB
第5个是ABAABABA
等等等等,
规律就是第N个字符串是第N-1个字符串与第N-2个字符串拼接而成,
现在要求输出N无限大时的字符串序列,就输出第N行就行,请问使用递归调用的函数吧?用C语言编的,不是C++,希望有高手给我解答一下,谢谢了,把程序发上来就可以了。
谢谢大家!

#include <stdio.h>
#include <string.h>
#include <malloc.h>

void f1(int n,char ** str)
{

if(n<3)
{
if(n==1)
{

strcpy(*str,"A");

}
if(n==2)
{

strcpy(*str,"AB");

}
}
else
{
char *tmp_str1,*tmp_str2;
tmp_str1=(char*)malloc(sizeof(char)*100);
tmp_str2=(char*)malloc(sizeof(char)*100);

f1(n-1,&tmp_str1);

f1(n-2,&tmp_str2);

strcpy(*str,tmp_str1);
strcat(*str,tmp_str2);

}

}
void main()
{
char * str;
str=(char*)malloc(sizeof(char)*1000);
f1(6,&str);
printf("%s\n",str);
}

VC调试通过

//---------------------------------------------------------------------------

#include <stdio.h>