简体   繁体   English

如何打印最多n个术语的序列,每个术语=前三个术语的总和,第一个术语= 1,然后2 3 6 11

[英]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 . 把值cs的,但你想要的值sc

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.
  1. The initial values should be 0,0,1 for a,b,c respectively a,b,c的初始值应分别为0,0,1
  2. In this case you will get an array of 1 2 4 7 13 在这种情况下,您将得到一个1 2 4 7 13的数组
  3. You have assigned the values for 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.

相关问题 我们如何使用 C 中的递归将系列 1+ 11 +111+... 的总和打印到 N 项 - How can we print the sum of series 1+ 11 +111+… upto N terms using recursion in C 系列:1 + 1/3 + 1/5 +…最多 N 项 - Series: 1 + 1/3 + 1/5 +…upto N terms 如何在 c 中打印最多 50 个项的斐波那契数列? - How to print fibonacci series upto 50 terms in c? 给定以下系列......该系列的第 n 项等于 (n - 1)th ^2 +1 并且该系列的第一项是 1 使用递归 - Given the following series... the nth term of the series equals to (n - 1)th ^2 +1 and the first term of the series is 1 use recursion 在方程中排列项以隔离C中的y项 - Arranging terms in an equation isolating the y term in C 用C语言编写一个程序,将输出给定数字之间的第一项和大量数字序列的项数 - Write a program in C that will output the first term and the number of terms of the sequence of abundant numbers between given numbers 打印 pi 的值。 在您第一次获得 3.14 之前,您必须使用这个系列的多少项? 3.141? 3.1415? 3.14159? - Print Value of pi. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159? C 对数级数前七项的程序 - C program for first seven terms of logarithmic series 编写程序以生成序列中的前n个项-34,18,10,6,4, - Write a program to generate the first n terms in the series — 34,18,10,6,4, 为什么通过添加无限级数的前 N 项来计算 sin(x) 总是返回 0.00000? - Why does calculating sin(x) by adding the first N terms of an infinite series always return 0.00000?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM