返回以逗号分隔的Fibonacci字符串 [英] returning the Fibonacci string separated by comma

查看:61
本文介绍了返回以逗号分隔的Fibonacci字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试编写一个斐波纳契递归函数,它将返回

由逗号分隔的斐波那契字符串。问题听起来像这样:

-------------

编写一个递归函数,创建一个包含
$的字符串b $ b前n个斐波纳契数 - F(n)= F(n - 1)+ F(n - 2),F(0)=

F(1)= 1 - ,分开用逗号。 n应作为程序的参数给出。
程序。递归函数应该只接受一个参数,n,

并且应该返回字符串。你不会使用任何额外的功能。


不要试图优化空间或速度。不要假设结果字符串的最大长度为
。另外,请不要使用全局/静态

变量。

-----------


代码必须在C中。我设法创建一个函数,将指定的''N'的

斐波纳契值作为char *返回,但我没有管理

以逗号分隔整个字符串。

这是我的函数:


char *递归(int n){

char * a = malloc(n * sizeof(char));

if(n == 0 || n == 1)

sprintf (a,%d,n);

else

sprintf(a,"%d",atoi(递归(n-1))+

atoi(递归(n-2)));

返回a;

}

我怎么能得到整个字符串?

提前感谢您的帮助!

解决方案

" mac" < a ************** @ gmail.comwrites:


我正在尝试写一个斐波纳契递归函数将返回

由逗号分隔的斐波那契字符串。问题听起来像这样:

-------------

编写一个递归函数,创建一个包含
$的字符串b $ b前n个斐波纳契数 - F(n)= F(n - 1)+ F(n - 2),F(0)=

F(1)= 1 - ,分开用逗号。 n应作为程序的参数给出。
程序。递归函数应该只接受一个参数,n,

并且应该返回字符串。你不会使用任何额外的功能。


不要试图优化空间或速度。不要假设结果字符串的最大长度为
。另外,请不要使用全局/静态

变量。

-----------


代码必须在C中。我设法创建一个函数,将指定的''N'的

斐波纳契值作为char *返回,但我没有管理

以逗号分隔整个字符串。

这是我的函数:


char *递归(int n){

char * a = malloc(n * sizeof(char));

if(n == 0 || n == 1)

sprintf (a,%d,n);

else

sprintf(a,"%d",atoi(递归(n-1))+

atoi(递归(n-2)));

返回a;

}



atoi在这里没用。你要做的是先将整数转换为

一个字符串,然后再转换成一个整数 - 浪费时间。而且,你永远不会释放你分配的内存。您应首先计算值

然后将其转换为字符串:


#v +

long fib(int n){

返回n< 2? 1:fib(n-1)+ fib(n-2);

}


char * fibstr(int n){

char * str = malloc(10); / * 10应该足够了* /

/ * FIXME:错误检查ommited * /

sprintf(str,"%ld",fib(n);

返回str;

}

#v-


然后你可以使用类似的东西:


#v +

for(i = 0; i< = n; ++ i){

char * str = fibstr( i);

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,"%s%s,", buf,str);

free(str);

}

buf [strlen(buf)] = 0;

#v-


甚至更好:


#v +

/ * FIXME:缓冲区溢出检查* /

char * fibstr(char * dest,int n){

sprintF(dest,"%ld",fib(n));

返回dest;

}


char * foo(int n){

char str [10] ;

/ * ... * /


for(i = 0; i< = n; ++ i){

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,& ;%s%s,",buf,fibstr(str,i));

}

buf [strlen(buf)] = 0;


/ * ... * /

}

#v-


-

祝你好运,_ _

.o。 | Serenly Enlightened Majesty of o'',=。/`o

..o |计算机科学,Michalmina86 Nazarewicz(oo)

ooo + - < mina86 * tlen.pl> ---< jid:mina86 * chrome.pl> - ooO - (_) - Ooo--


Michal Nazarewicz写道:


" mac" < a ************** @ gmail.comwrites:


>我正在尝试写一个斐波纳契递归函数将返回由逗号分隔的斐波那契字符串。问题听起来像这样:
-------------
写一个递归函数,创建一个包含
前n个Fibonacci数字的字符串--F(n )= F(n-1)+ F(n-2),F(0)=
F(1)= 1 - ,用逗号分隔。 n应该作为程序的参数。递归函数应该只接受一个参数n,
并且应该返回字符串。你不会使用任何额外的功能。

不要试图优化空间或速度。不要假设结果字符串有任何最大长度。另外,请不要使用全局/静态
变量。
-----------

代码必须在C中。我设法创建一个函数,返回指定的''N'的
fibonacci值作为char *,但我没有管理
以逗号分隔整个字符串。
这是我的功能:

char * Recursive(int n){
char * a = malloc(n * sizeof(char));
if(n == 0 || n == 1)
sprintf(a,"%d",n);
其他
sprintf(a,"%d",atoi(递归(n-1)) +
atoi(递归(n-2)));
返回;
}



atoi在这里没用。

你要做的是先将整数转换成

a字符串然后再转换成整数 - 浪费时间。



不要试图优化空间或速度。


此外,你

永远不会释放你分配的内存。



不要试图优化空间或速度。


你应该先计算值

然后将其转换为字符串:


#v +

long fib(int n){

返回n< 2? 1:fib(n-1)+ fib(n-2);

}



"你不会使用任何额外的功能。"


char * fibstr(int n){

char * str = malloc(10); / * 10应该足够了* /

/ * FIXME:错误检查ommited * /

sprintf(str,"%ld",fib(n);

返回str;

}

#v-

然后你可以使用类似的东西:


#v +

for(i = 0; i< = n; ++ i){

char * str = fibstr(i);

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,"%s%s,",buf,str) ;

免费(str);

}

buf [strlen(buf)] = 0;



什么?


#v-


甚至更好:


#v +

/ * FIXME:缓冲区溢出检查* /

char * fibstr(char * dest,int n){

sprintF(dest,"%ld",fib(n));

返回dest;

}


char * foo(int n){

char str [10];

/ * ... * /


for(i = 0; i< = n; ++ i){

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,"%s%s,",buf,fibstr(str,i));

}

buf [strlen(buf)] = 0;


/ * ... * /

}

#v-



嗯,你们四处走动都是因为相对而言

直截了当的问题,并忽略解决方案条件

来启动。 (我假设额外功能你不被允许

使用不是C库函数 - 否则问题是

不可溶......)


-

Chris电刺猬 Dollin

快捷方式都是人们使用它们。




Chris Dollin写道:


Michal Nazarewicz写道:


" mac" < a ************** @ gmail.comwrites:


我正在尝试写一个斐波纳契递归函数将返回

由逗号分隔的斐波那契字符串。问题听起来像这样:

-------------

编写一个递归函数,创建一个包含
$的字符串b $ b前n个斐波纳契数 - F(n)= F(n - 1)+ F(n - 2),F(0)=

F(1)= 1 - ,分开用逗号。 n应作为程序的参数给出。
程序。递归函数应该只接受一个参数,n,

并且应该返回字符串。你不会使用任何额外的功能。


不要试图优化空间或速度。不要假设结果字符串的最大长度为
。另外,请不要使用全局/静态

变量。

-----------


代码必须在C中。我设法创建一个函数,将指定的''N'的

斐波纳契值作为char *返回,但我没有管理

以逗号分隔整个字符串。

这是我的函数:


char *递归(int n){

char * a = malloc(n * sizeof(char));

if(n == 0 || n == 1)

sprintf (a,%d,n);

else

sprintf(a,"%d",atoi(递归(n-1))+

atoi(递归(n-2)));

返回a;

}



atoi在这里没用。

你要做的是先将整数转换成

a字符串然后再转换成整数 - 浪费时间。



不要尝试优化空间或速度。


此外,你

永远不会释放你分配的内存。



不要试图优化空间或速度。


你应该先计算值

然后将其转换为字符串:


#v +

long fib(int n){

返回n< 2? 1:fib(n-1)+ fib(n-2);

}



"你不会使用任何额外的功能。"


char * fibstr(int n){

char * str = malloc(10); / * 10应该足够了* /

/ * FIXME:错误检查ommited * /

sprintf(str,"%ld",fib(n);

返回str;

}

#v-

然后你可以使用类似的东西:


#v +

for(i = 0; i< = n; ++ i){

char * str = fibstr(i);

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,"%s%s,",buf,str) ;

免费(str);

}

buf [strlen(buf)] = 0;



什么?


#v-


甚至更好:


#v +

/ * FIXME:缓冲区溢出检查* /

char * fibstr(char * dest,int n){

sprintF(dest,"%ld",fib(n));

返回dest;

}


char * foo(int n){

char str [10];

/ * ... * /


for(i = 0; i < = N; ++ i){

/ * FIXME:需要检查buffor大小和realloc()如果需要* /

sprintf(buf,"%s%s,& ;,buf,fibstr(str,i));

}

buf [strlen(buf)] = 0;


/ * ... * /

}

#v-



嗯,你是在房子周围走动,这是一个相对简单的问题,并忽略解决方案条件

来启动。 (我假设额外功能你不被允许

使用不是C库函数 - 否则问题是

不可溶......)


-

Chris电刺猬 Dollin

快捷方式全都是人们使用它们。



C库函数是允许的...我正在使用strcat,atoi,

itoa ....但仍然没有我设法做对了......我已经接近了解决方案......但是却没有工作


Hi,

I''m trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don''t use global / static
variables.
-----------

The code must be in C. I managed to create a function that returns the
fibonacci value for the specified ''N'' as a char*, but I didn''t manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof(char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive(n-1)) +
atoi(Recursive(n-2)));
return a;
}
How could I get the entire string?
Thanks in advance for help!

解决方案

"mac" <an**************@gmail.comwrites:

I''m trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don''t use global / static
variables.
-----------

The code must be in C. I managed to create a function that returns the
fibonacci value for the specified ''N'' as a char*, but I didn''t manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof(char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive(n-1)) +
atoi(Recursive(n-2)));
return a;
}

atoi is useless here. What you are doing is first convert integer into
a string and then back into an integer - waste of time. Moreover, you
never free memory you allocate. You should first calculate the value
and then convert it into string:

#v+
long fib(int n) {
return n<2 ? 1 : fib(n-1) + fib(n-2);
}

char *fibstr(int n) {
char *str = malloc(10); /* 10 should be enough */
/* FIXME: error checking ommited */
sprintf(str, "%ld", fib(n);
return str;
}
#v-

Then you can use something like:

#v+
for (i = 0; i<=n; ++i) {
char *str = fibstr(i);
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, str);
free(str);
}
buf[strlen(buf)] = 0;
#v-

or even better:

#v+
/* FIXME: buffer overflow checking */
char *fibstr(char *dest, int n) {
sprintF(dest, "%ld", fib(n));
return dest;
}

char *foo(int n) {
char str[10];
/* ... */

for (i = 0; i<=n; ++i) {
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, fibstr(str, i));
}
buf[strlen(buf)] = 0;

/* ... */
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o'' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--


Michal Nazarewicz wrote:

"mac" <an**************@gmail.comwrites:

>I''m trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don''t use global / static
variables.
-----------

The code must be in C. I managed to create a function that returns the
fibonacci value for the specified ''N'' as a char*, but I didn''t manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof(char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive(n-1)) +
atoi(Recursive(n-2)));
return a;
}


atoi is useless here.
What you are doing is first convert integer into
a string and then back into an integer - waste of time.

"Do not try to optimize for space or speed."

Moreover, you
never free memory you allocate.

"Do not try to optimize for space or speed."

You should first calculate the value
and then convert it into string:

#v+
long fib(int n) {
return n<2 ? 1 : fib(n-1) + fib(n-2);
}

"You will not use any extra function."

char *fibstr(int n) {
char *str = malloc(10); /* 10 should be enough */
/* FIXME: error checking ommited */
sprintf(str, "%ld", fib(n);
return str;
}
#v-
Then you can use something like:

#v+
for (i = 0; i<=n; ++i) {
char *str = fibstr(i);
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, str);
free(str);
}
buf[strlen(buf)] = 0;

What?

#v-

or even better:

#v+
/* FIXME: buffer overflow checking */
char *fibstr(char *dest, int n) {
sprintF(dest, "%ld", fib(n));
return dest;
}

char *foo(int n) {
char str[10];
/* ... */

for (i = 0; i<=n; ++i) {
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, fibstr(str, i));
}
buf[strlen(buf)] = 0;

/* ... */
}
#v-

Well, you''re going all around the houses for what''s a relatively
straightforward problem, and ignoring the solution conditions
to boot. (I''m assuming the "extra functions" you''re not allowed
to use aren''t C library functions -- otherwise the problem is
insoluble ...)

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.



Chris Dollin wrote:

Michal Nazarewicz wrote:

"mac" <an**************@gmail.comwrites:

I''m trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don''t use global / static
variables.
-----------

The code must be in C. I managed to create a function that returns the
fibonacci value for the specified ''N'' as a char*, but I didn''t manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof(char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive(n-1)) +
atoi(Recursive(n-2)));
return a;
}

atoi is useless here.
What you are doing is first convert integer into
a string and then back into an integer - waste of time.


"Do not try to optimize for space or speed."

Moreover, you
never free memory you allocate.


"Do not try to optimize for space or speed."

You should first calculate the value
and then convert it into string:

#v+
long fib(int n) {
return n<2 ? 1 : fib(n-1) + fib(n-2);
}


"You will not use any extra function."

char *fibstr(int n) {
char *str = malloc(10); /* 10 should be enough */
/* FIXME: error checking ommited */
sprintf(str, "%ld", fib(n);
return str;
}
#v-
Then you can use something like:

#v+
for (i = 0; i<=n; ++i) {
char *str = fibstr(i);
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, str);
free(str);
}
buf[strlen(buf)] = 0;


What?

#v-

or even better:

#v+
/* FIXME: buffer overflow checking */
char *fibstr(char *dest, int n) {
sprintF(dest, "%ld", fib(n));
return dest;
}

char *foo(int n) {
char str[10];
/* ... */

for (i = 0; i<=n; ++i) {
/* FIXME: need to check buffor size and realloc() if needed */
sprintf(buf, "%s%s,", buf, fibstr(str, i));
}
buf[strlen(buf)] = 0;

/* ... */
}
#v-


Well, you''re going all around the houses for what''s a relatively
straightforward problem, and ignoring the solution conditions
to boot. (I''m assuming the "extra functions" you''re not allowed
to use aren''t C library functions -- otherwise the problem is
insoluble ...)

--
Chris "electric hedgehog" Dollin
The shortcuts are all full of people using them.

The C library functions are allowed ... I''m using strcat, atoi,
itoa .... but still didn''t manage to get it right ... I fell I''m close
to the solutions ... but yet doesn''t work


这篇关于返回以逗号分隔的Fibonacci字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆