[英]How to print a series upto n terms, with each term = sum of previous three terms and first term=1 then 2 3 6 11
I am not understanding why my code isn't showing right output. 我不明白为什么我的代码没有显示正确的输出。 For example: input of n=5
should give output 1 2 3 6 11
but it gives output 1 1 0 0 0
. 例如: n=5
输入应给输出1 2 3 6 11
但它给输出1 1 0 0 0
。 Can you please suggest some improvement in my code? 能否请您提出一些改进建议?
My code works on the principle similar to Fibonacci series. 我的代码的工作原理类似于斐波那契数列。 Instead of adding 2 previous terms it adds previous 3 terms. 而不是添加两个以前的术语,而是添加前面的三个术语。
#include <stdio.h>
int main() {
int a=0,b=1,c=0,i,n,s; //To print s=1(first term)
scanf("%d",&n);
for(i=1;i<=n;i++){
s=a+b+c;
printf("%d ",s);//for first time it will print 1.
s=c;//for second term value of s=1 will be pasted in c.
c=b; //for second term value of c=0 will be pasted in b.
b=a; //for second term value of b=1 will be pasted in a.
} //loop will go back to first step and print s=1+0+1=2(second term)
and so on ,3,6,11....
return 0;
}
I expect the output for n=5
to be 我希望n=5
的输出是
1,2,3,6,11 1,2,3,6,11
but actual output is 但实际输出是
1 1 0 0 0 1 1 0 0 0
You have assigned the values incorrectly. 您分配的值不正确。 You are also assigning s = c, which is incorrect as it is not being used in future. 您还分配了s = c,这是不正确的,因为将来不再使用它。
#include <stdio.h>
int main() {
int a=0,b=1,c=0,i,n,s; //To print s=1(first term)
scanf("%d",&n);
for(i=1;i<=n;i++){
s=a+b+c;
printf("%d ",s);//for first time it will print 1.
// The values you were swapping was in reverse order.
a = b;
b = c;
c = s;
}
return 0;
}
You have the assignment statements reversed. 您有相反的分配语句。 s=c;
puts the value of c
is s
, but you want the value of s
in c
. 把值c
是s
的,但你想要的值s
在c
。
You can fix that by reversing each assignment, but then you will be changing c
before it is stored in b
and changing b
before it is stored in a
. 您可以修复,通过倒车每项任务,但随后你会改变c
它存储在之前b
和改变b
它存储在之前a
。 To fix that, reverse the order of the assignments, so a
is assigned first: 要解决此问题,请颠倒分配顺序,因此首先分配a
:
a = b;
b = c;
c = s;
You have your assignments the wrong way around: 您的作业分配错误:
s=c;//for second term value of s=1 will be pasted in c.
c=b; //for second term value of c=0 will be pasted in b.
b=a; //for second term value of b=1 will be pasted in a.
This should be 这应该是
a = b; //for second term value of b=1 will be pasted in a.
b = c; //for second term value of c=0 will be pasted in b.
c = s;//for second term value of s=1 will be pasted in c.
0,0,1
for a,b,c
respectively a,b,c
的初始值应分别为0,0,1
1 2 4 7 13
在这种情况下,您将得到一个1 2 4 7 13
的数组 a,b,c
in the reverse order. 您已按照相反的顺序分配了a,b,c
的值。 The modified code is below. 修改后的代码如下。
int main()
{
int a=0,b=0,c=1,i,n,s; //To print s=1(first term)
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=a+b+c;
printf("%d ",s);//for first time it will print 1.
a=b;
b=c;
c=s;
}
return 0;
}
Output for n = 5 is n = 5的输出是
1 2 4 7 13 1 2 4 7 13
Edit 编辑
To print the value of nth term, you need to move the printf
to the end of the loop. 要打印第n个项的值,您需要将printf
移动到循环的末尾。
If n
is very large, you can consider not having a loop itself, deriving the formula for the nth term and using that directly. 如果n
非常大,则可以考虑不具有循环本身,导出第n个项的公式并直接使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.