斐波那契数列C程序错误
Fibonacci Sequence C program error
我正在尝试编写一个程序,它将斐波那契数列的前 2 个数字作为输入以及 n
的值。然后程序给出斐波那契数列第n位的输出。
#include <stdio.h>
#include <stdlib.h>
int main () {
int n, i;
int s[n - 1];
int a, b;
printf("Enter two first two numbers:");
scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
for (i = 2; i <= n - 1; i++) {
s[i] = s[i - 1] + s[i - 2];
}
printf("The nth digit is %d", s[n - 1]);
return(0);
}
我得到的是答案编号,后面跟着一些额外的任意数字
在这里你定义了一个未知大小的数组,幸运的是 n
不会恰好是 0 或 1 或负数。
int s[n-1];
在这里你忽略了 scanf 的 return 值,你真的应该检查它以验证扫描是否成功。
scanf("%d %d",&a,&b);
scanf("%d",&n);
即使假设一个有意义定义的数组,您也可以设置一个循环以在此处生成超出数组的索引:
for (i=2 ; i<=n-1 ; i++)
然后你在数组之外(在循环的最后一次迭代期间)写在这里:
s[i]=
有了这段代码,所有的赌注都被取消了,你保证了未定义的行为,因此任何解释到底出了什么问题都是徒劳的。
几件事。如前所述,您正试图在 n
被赋值之前使用它。此外,在使用变量确定数组大小时,应使用 malloc()
。
接下来,如果您要计算第 n 个和,那么您需要数组有 n
个元素,而不是 n-1
第三,您读入了两个起始值 a
和 b
,但您从未使用它们来初始化数组的前两个元素。
最后,您需要修复循环索引。 (实际上,一旦您将数组更改为具有 n
元素而不是 n-1
元素,您的索引就可以了,但是,肯定更喜欢使用 i < n
而不是 i <= n-1
)
int main() {
int n, i;
int a, b;
printf("Enter two first two numbers:");
scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
int *s = malloc(n * sizeof(*s));
s[0] = a;
s[1] = b;
for (i = 2; i < n; i++) {
s[i] = s[i - 1] + s[i - 2];
}
printf("The nth digit is %d", s[n - 1]);
return(0);
}
实际上实现您的代码不需要数组 s[]
.
这可以简单地实现为:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int a, b;
printf("Enter two first two numbers:");
scanf("%d%d", &a, &b); // not scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
for (i = 1; i < n; i++)
{
b += a;
a = b - a;
}
printf("The nth digit is %d\n", a);
return (0);
}
输出:
Enter two first two numbers:0 1
Enter the value of n(3-100):5
The nth digit is 3 // 0 1 1 2 3
我正在尝试编写一个程序,它将斐波那契数列的前 2 个数字作为输入以及 n
的值。然后程序给出斐波那契数列第n位的输出。
#include <stdio.h>
#include <stdlib.h>
int main () {
int n, i;
int s[n - 1];
int a, b;
printf("Enter two first two numbers:");
scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
for (i = 2; i <= n - 1; i++) {
s[i] = s[i - 1] + s[i - 2];
}
printf("The nth digit is %d", s[n - 1]);
return(0);
}
我得到的是答案编号,后面跟着一些额外的任意数字
在这里你定义了一个未知大小的数组,幸运的是 n
不会恰好是 0 或 1 或负数。
int s[n-1];
在这里你忽略了 scanf 的 return 值,你真的应该检查它以验证扫描是否成功。
scanf("%d %d",&a,&b);
scanf("%d",&n);
即使假设一个有意义定义的数组,您也可以设置一个循环以在此处生成超出数组的索引:
for (i=2 ; i<=n-1 ; i++)
然后你在数组之外(在循环的最后一次迭代期间)写在这里:
s[i]=
有了这段代码,所有的赌注都被取消了,你保证了未定义的行为,因此任何解释到底出了什么问题都是徒劳的。
几件事。如前所述,您正试图在 n
被赋值之前使用它。此外,在使用变量确定数组大小时,应使用 malloc()
。
接下来,如果您要计算第 n 个和,那么您需要数组有 n
个元素,而不是 n-1
第三,您读入了两个起始值 a
和 b
,但您从未使用它们来初始化数组的前两个元素。
最后,您需要修复循环索引。 (实际上,一旦您将数组更改为具有 n
元素而不是 n-1
元素,您的索引就可以了,但是,肯定更喜欢使用 i < n
而不是 i <= n-1
)
int main() {
int n, i;
int a, b;
printf("Enter two first two numbers:");
scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
int *s = malloc(n * sizeof(*s));
s[0] = a;
s[1] = b;
for (i = 2; i < n; i++) {
s[i] = s[i - 1] + s[i - 2];
}
printf("The nth digit is %d", s[n - 1]);
return(0);
}
实际上实现您的代码不需要数组 s[]
.
这可以简单地实现为:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int a, b;
printf("Enter two first two numbers:");
scanf("%d%d", &a, &b); // not scanf("%d %d", &a, &b);
printf("Enter the value of n(3-100):");
scanf("%d", &n);
for (i = 1; i < n; i++)
{
b += a;
a = b - a;
}
printf("The nth digit is %d\n", a);
return (0);
}
输出:
Enter two first two numbers:0 1
Enter the value of n(3-100):5
The nth digit is 3 // 0 1 1 2 3