计算斐波那契数列的第n个数,其中n在命令行输入
我想编写一个程序来计算fibonnacci序列的第n个数,我已经使用printf和scanf完成了这个数。但我希望改变我的程序,以便序列号在命令行输入,而不是在程序提示时输入。这是我想到的。它编译,但当它运行时崩溃......不知道为什么。任何建议,将不胜感激。计算斐波那契数列的第n个数,其中n在命令行输入
这是一个计算使用迭代计算fibonnacci代码的第n个数字的程序。我已经这样写了: 您必须在命令行argv [1]中输入您希望计算的序列号。程序然后使用这个命令行 参数并在while循环中使用它,并且打印这个数字。
#include <stdio.h>
int main(int argc, char**argv) {
int fib[3] = {0,1};
int counter = 0;
printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
while (counter < atoi(argv[1])) {
fib[2] = fib[0] + fib[1];
fib[0] = fib[1];
fib[1] = fib[2];
counter++;
}
printf("%d\n", fib[0]);
getchar();
return 0;
}
程序应该检查是否有命令行参数存在:
if (argc < 2)
{
printf ("usage: %s n\n where n is a positive integer\n", argv[0]);
return 1;
}
如果没有提供任何参数,它可能会崩溃。
= = = = = =编辑= = = =
我看不到任何原因崩溃,一旦上面的错误是固定的。这工作正常:
#include <stdio.h>
int main(int argc, char**argv)
{
int fib[3] = {0,1};
int counter = 0;
if (argc < 2)
{
printf ("usage: %s number\n", argv[0]);
return 1;
}
printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
while (counter < atoi(argv[1]))
{
fib[2] = fib[0] + fib[1];
fib[0] = fib[1];
fib[1] = fib[2];
counter++;
}
printf("%d\n", fib[0]);
return 0;
}
[[email protected] ~]$ ./a.out
usage: ./a.out number
[[email protected] ~]$ ./a.out 3
The 3th Fibonacci number is:
2
[[email protected] ~]$ ./a.out 4
The 4th Fibonacci number is:
3
[[email protected] ~]$ ./a.out 5
The 5th Fibonacci number is:
5
[[email protected] ~]$ ./a.out 6
The 6th Fibonacci number is:
8
[[email protected] ~]$ ./a.out 7
The 7th Fibonacci number is:
13
[[email protected] ~]$ ./a.out 8
The 8th Fibonacci number is:
21
[[email protected] ~]$ ./a.out 9
The 9th Fibonacci number is:
34
[[email protected] ~]$ ./a.out 10
The 10th Fibonacci number is:
55
你测试了什么数字?因为一个int将不能保存高于〜50th斐波那契数的任何东西。我必须在我的算法类中做这样的事情,我们必须找到第239个Fibonacci数字,这是64202014863723094126901777428873111802307548623680
这是一个int或long都可以正确保存的东西。我不认为这会让它崩溃,而只是给出如果你需要找到一个大的斐波那契数字,请提出来。
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int *numberOfTerms;
int x,y,z,i,count;
if(c==1)
{
printf("provide number of terms as command line argument");
return 0;
}
numberOfTerms=(int *)malloc(sizeof(int));
*numberOfTerms=atoi(v[1]);
x=0;
y=1;
if(*numberOfTerms==0) return 0;
if(*numberOfTerms<=2)
{
for(i=0;i<*numberOfTerms;i++) printf("%d\n",i);
return 0;
}
printf("%d\n",x);
printf("%d\n",y);
count=2;
while(count<*numberOfTerms)
{
z=x+y;
printf("%d\n",z);
x=y;
y=z;
count++;
}
}
一些与代码一起解释将非常赞赏我认为 – 2014-08-10 04:32:19
这是一个打印斐波那契数列最多n个项目的程序,由用户提供的命令行参数..数字存储在char []中..所以它需要被转换为int ..对于这个我已经使用了预定义的函数“atoi”..并且必须包含这个stdlib – Priyanka 2014-08-16 09:58:17
如果你要打印的系列高达指定命令行参数一定数量,然后再为这个项目:
#include<stdio.h>
#include<stdlib.h>
int main(int c,char *v[])
{
int *number;
int x,y,z;
if(c==1)
{
printf("provide number upto which febonacci is to be printed (greater than 2)");
return 0;
}
number=(int *)malloc(sizeof(int));
*number=atoi(v[1]);
x=0;
y=1;
printf("%d\n",x);
printf("%d\n",y);
z=x+y;
while(z<=*number)
{
printf("%d\n",z);
x=y;
y=z;
z=x+y;
}
}
嗯。为什么我永远不能把所有的代码都放在一个蓝色的盒子里,让它看起来很体面! – none 2011-03-15 16:28:55
对代码文本使用Ctrl-K格式选项。 – wallyk 2011-03-15 16:30:44
好吧,从现在开始,谢谢! – none 2011-03-15 16:32:22