在 C 中递归计算斐波那契数
我正在尝试通过编写一个简单的程序来输出斐波那契数来学习C。它不起作用。
fibonacci.h
unsigned int fibonacci_recursive(unsigned int n);
fibonacci.c
#include <stdio.h>
#include "fibonacci.h"
main() {
unsigned int i;
for (i = 0; i < 10; i++) {
printf("%d\t%n", fibonacci_recursive(i));
}
getchar();
}
fibonacci_recursive.c
unsigned int fib_rec(unsigned int n);
main(unsigned int n) {
return fib_rec(n);
}
unsigned int fib_rec(unsigned int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fib_rec(n - 1) + fib_rec(n - 2);
}
这是当我尝试构建项目时 VS 2010 给我的错误消息:
1>ClCompile:
1> fibonacci_recursive.c
1>fibonacci_recursive.obj : error LNK2005: _main already defined in fibonacci.obj
1>fibonacci.obj : error LNK2019: unresolved external symbol _fibonacci_recursive referenced in function _main
1>c:\users\odp\documents\visual studio 2010\Projects\Fibonacci\Debug\Fibonacci.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
What am I这里做错了吗?感谢您帮助刚接触 C 的人。
I'm trying to learn C by writing a simple program to output Fibonacci numbers. It isn't working.
fibonacci.h
unsigned int fibonacci_recursive(unsigned int n);
fibonacci.c
#include <stdio.h>
#include "fibonacci.h"
main() {
unsigned int i;
for (i = 0; i < 10; i++) {
printf("%d\t%n", fibonacci_recursive(i));
}
getchar();
}
fibonacci_recursive.c
unsigned int fib_rec(unsigned int n);
main(unsigned int n) {
return fib_rec(n);
}
unsigned int fib_rec(unsigned int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fib_rec(n - 1) + fib_rec(n - 2);
}
This is the error message VS 2010 gives me when I try to build the project:
1>ClCompile:
1> fibonacci_recursive.c
1>fibonacci_recursive.obj : error LNK2005: _main already defined in fibonacci.obj
1>fibonacci.obj : error LNK2019: unresolved external symbol _fibonacci_recursive referenced in function _main
1>c:\users\odp\documents\visual studio 2010\Projects\Fibonacci\Debug\Fibonacci.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
What am I doing wrong here? Thanks for helping someone new to C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的方法看起来很奇怪,你应该有:
main.c
),其中包括fibonacci.h
fibonacci.h< /code> 带有原型
unsigned int fibonacci_recursive(unsigned int n);
fibonacci.c
,并且它应该包含fibonacci.h
也实际上你也定义了两次
main
函数..main.c
fibonacci.h
fibonacci.c
Your approach seems strange, you should have:
main.c
) with the main method and that includesfibonacci.h
fibonacci.h
with the prototypeunsigned int fibonacci_recursive(unsigned int n);
fibonacci.c
with the implementation of the method, and it should includefibonacci.h
tooActually you define
main
function twice too..main.c
fibonacci.h
fibonacci.c
您在项目中定义了两次
main()
函数。这是您的程序的入口点,您只需要一个。You have the
main()
function defined twice in your project. This is the entry point of your program, and you only need one.你的 printf 需要 \n 而不是 %n 。另外,您可以简化为:
You need \n not %n for your printf. Also, you can simplify as:
您尚未创建在 fibonacci.h 中声明的 fibonacci_recursive 函数。
You haven't created a fibonacci_recursive function that you declared in fibonacci.h.
您声明了两个
main()
函数,换行符为 '\n'。You declared two
main()
functions, and the new line character is '\n'.好吧,我先说递归函数不是计算斐波那契的有效方法,它可能仅用于开发培训/演示目的,因为每个递归都存储在堆栈中,并且对于大斐波那契数也可能溢出。
编写一个使用循环的更高效的斐波那契函数是相当值得的,例如下面的代码:
尝试通过您自己的比较,使用此方法运行 ./fibonacci 50,例如在低成本处理器(例如在 Raspberry PI 上),以及具有递归函数和 50 个第一个数字的处理器,看看有什么不同! ,-)
Well, I preface that recursive function is not an efficient method to calculate Fibonacci and it may be used for dev training/demonstrations purposes only, because every recursion is stored in stack, and it may also overflow for large fibonacci numbers.
It is rather worth the effort to write down a more efficient Fibonacci function that uses a loop, like following code:
Try to compare by your own, running ./fibonacci 50 with this method, for instance on a low cost processor (eg. on a Raspberry PI), and the one with the recursive functions and 50 first numbers as well, and see the difference! ,-)