一点拙见,希望大佬可以评论下意见

/*牛牛数列6*/
#include<iostream>
using namespace std;
int main()
{
    long int d, a=0,b=1,c=1;
    int n;
    cin>>n;
    if(n == 1) cout<<0;
    else if(n == 2||n == 3)
    cout<<1;
    else
    {
        for(int i = 4; i <= n; i++)
        {    
           d=a+2*b+c;
            a=b;
            b=c;
            c=d;
                
            //a=a+2*b+c;        //这里我认为可以少用一个d,但是只对了百分之40,所以就有了上面的
            //b=c+b;
            //c=b-c;
            //b=b-c;
        }
    }
    cout<<d<<endl; 
    return 0;
}
全部评论
这个题我认为使用递归函数更方便一点 #include<iostream> using namespace std; int F(int n); int main() {     int n;     cin>>n;     cout<<F(n);      } int F(int n) {     int sum=0;     if(n==1)     {         return 0;     }     else if(n==2||n==3)     {         return 1;     }     else     {         sum=F(n-3)+2*F(n-2)+F(n-1);     }     return sum;           }
点赞 回复
分享
发布于 2019-12-31 17:52
请问我这样写咋就不对呐 #include<iostream> using namespace std; int main() {     int a[1000];     a[1]=0;a[2]=1;a[3]=1;     int x;     cin >> x;     if(x==1) cout<< 0 << endl;     if(x==2 || x==3) cout << 1 << endl;     if(x>=3)     {         int i;         for(i=4;i<=x;i++)         {             a[i]=a[i-3]+2*a[i-2]+a[i-1];         }         cout << a[i] << endl;     }     return 0; }
点赞 回复
分享
发布于 2020-04-09 15:07
联易融
校招火热招聘中
官网直投

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务