94 条题解

  • 0
    @ 2023-7-3 15:15:47
    #include<stdio.h>
    #include "stdlib.h"
    
    int F(int n){
        if(n==1 || n==2 || n==3){
            return 1;
        }
        return F(n-1)+F(n-2)+F(n-3);
    }
    
    int main(){
        int n;
        scanf("%d",&n);
        int res=F(n);
        printf("%d",res);
        system("pause");
    }
    
    • 0
      @ 2023-6-18 19:48:29
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
      	int n,a[50];
      	cin >> n;
      	a[1] = 1;
      	a[2] = 1;
      	a[3] = 1;
      	for (int i = 4;i <= n;i++)
      	{
      		a[i] = a[i - 1] + a[i - 2] + a[i - 3]; 
      	}
      	cout << a[n];
      	return 0;
      }
      
      
      • 0
        @ 2023-6-17 18:48:12
        # 代码不怎么高大上,是因为为了让你们更好理解。(你信么?)
        #include <iostream>
        using namespace std;
        int main()
        {
            long long a;
            cin>>a;
            long long c=1;
            long long j=1,y=1,b=1;
            for (int i=3;i<=a-1;i++)
            {
                c=j+y+b;
                j=y;
                y=b;
                b=c;
            }
            cout<<c;
            return 0;
        }//不要直接拿去复制,看懂后自己敲一遍。
        
        
        
        • @ 2023-8-6 12:15:57

          //我的理解(禁止抄袭) #include <iostream> using namespace std; int main() { int a;//定义 cin>>a;//输入 int c=1;//防止系统给一个很大的数(和int c=0;类似) int j=1,y=1,b=1;//j,y,b,分别是题目给出的第一、二、三个(题目中的1,1,1,数列中的前三项) for (int i=3;i<=a-1;i++) { c=j+y+b;//前三项相加(数列中的第四个) j=y;//(目前)第一个等于(目前)第二个 y=b;//(目前)第二个等于(目前)第三个 b=c;//(目前)第三个等于(目前)第四个 //每一项等于下一项 } cout<<c;//输出题目中说的"第n项" return 0; }//为了方便理解,所有long long改为了int

      • 0
        @ 2023-6-17 18:41:18
        int func(int n){
        如果(n==1 或 n==2 或 n==3)
            返回 1;
        返回 func(n-1)+func(n-2)+func(n-3);
        }
        
        • 0
          @ 2023-5-28 11:59:59

          #include<bits/stdc++.h> using namespace std; int a; int ff(int a) { if(a1 || a2 || a==3) return 1; return(ff(a-1)+ff(a-2)+ff(a-3)); } int main() { cin>>a; cout<<ff(a); }

          • 0
            @ 2023-5-13 22:13:07

            先看规律: 其实就是每个数都是自己前三项的和, 第n项=n-1项+n-2项+n-3项 代码如下:

            #include<iostream>
            using namespace std;
            int m=0;
            int func(int n)
            {
                if(n==1)return 1;
                if(n==2)return 1;
                if(n==3)return 1;
                m=func(n-3)+func(n-2)+func(n-1);
                return m;
            }
            int main()
            {
                int n;
                cin>>n;
                cout<<func(n);
            }
            ```(请放心食用,点赞再用哦\~)
            
            • 0
              @ 2023-5-13 22:01:46

              这道题目是一道递推题,需要找到数列的规律,然后根据规律递推出第n项的值。

              我们可以发现,数列中每一项都是前三项之和。那么我们可以用a、b、c三个变量来表示前三项,用ans表示当前项的值,然后用一个for循环从第四项开始递推到第n项,每次更新前三项的值,并根据前三项的值求出当前项的值。

              具体来说,我们可以将前三项初始化为1,即a=1,b=1,c=1,第一项、第二项、第三项都是1。然后从第四项开始,用ans=a+b+c求出当前项的值,然后更新前三项,即a=b,b=c,c=ans。最后输出第n项的值即可。


              代码如下:

              #include <iostream>
              
              using namespace std;
              
              int main()
              {
                  int n;
                  cin >> n;
                  int a = 1, b = 1, c = 1; // 前三项
                  int ans = 1;
                  for (int i = 4; i <= n; i++) {
                      ans = a + b + c; // 每一项都是前三项之和
                      a = b;
                      b = c;
                      c = ans; // 更新前三项
                  }
                  cout << ans << endl; // 输出第n项
                  return 0;
              }
              
              • @ 2023-8-3 20:28:45

                下次好好审题(章节题目上说是递归思想)

            • 0
              @ 2023-5-13 20:48:18

              首先观察数据,发现规律:

              a[k]=a[k-3]+a[k-2]+a[k-1]

              其中

              a[1]=a[2]=a[3]=1

              然后编写递归代码:

              int func(int x){
                  if(x<=3)return 1;
                  return func(x-3)+func(x-2)+func(x-1);
              

              最后

              main()中调用

              即可

              • 0
                @ 2023-5-13 11:52:11

                先给出代码:

                #include <bits/stdc++.h>
                #define int long long
                using namespace std;
                int n;
                int dfs(int x)
                {
                    if(x==1 ||x==2 || x==3)return 1;
                    return dfs(x-1)+dfs(x-2)+dfs(x-3);
                }
                signed main()
                {
                    cin>>n;
                    cout<<dfs(n);
                    return 0;
                }
                

                思路:

                其实这就是一个简单的找规律而已。

                111359173157105……

                先看第四项,它是前三项的和;

                再看第五项,它是第2,3,4项的和;

                再看第x项,它是第x-1,x-2,x-3的和。

                • 0
                  @ 2023-5-12 20:59:25
                  #include<iostream>
                  using namespace std;
                  int n;
                  int func(int i)//定义函数
                  {
                      if (i == 1 || i == 2 || i == 3){//和斐波那契数列很像
                          return 1;
                      }
                      return func(i - 1) + func(i - 2) + func (i - 3);//递归
                  }
                  int main()
                  {
                      cin >> n;
                      cout << func(n);
                  }
                  
                  • 0
                    @ 2023-4-2 20:09:24

                    抱歉老师!我真的想用推递! #include <bits/stdc++.h> using namespace std; int n , a[100000]; int main() { cin >> n; a[1] = 1; a[2] = 1; a[3] = 1; for(int i = 4;i <= n;i++){ a[i] = a[i - 1] + a[i - 2] + a[i - 3]; } cout << a[n]; return 0; }

                    • @ 2023-4-4 5:25:47

                      哈哈 没问题,不过作为这节课的课后练习,尽量使用课上知识来编写,巩固学习。

                      另外,同学注意格式哦~

                      下周同一时间,我会重新检查,如果未修改,我会删除这篇题解。

                  • 0
                    @ 2023-3-11 14:34:00
                    #include <iostream>
                    using namespace std;
                    int a[100]; 
                    int main()
                    {
                        int n;
                        cin>>n;
                        a[0]=1;
                        a[1]=1;
                        a[2]=1;
                        for (int i=3;i<n;i++)
                        {
                            a[i]=a[i-1]+a[i-2]+a[i-3];
                        }
                        cout<<a[n-1];
                        return 0;//哈哈嗨!请放心食用!
                    }
                    
                    • 0
                      @ 2023-3-5 17:02:33

                      image 我哪错了吗?

                      • @ 2023-3-9 15:31:26

                        定义函数的时候,需要遵循以下规则:

                        [<类型>]<函数名> (<类型1> <形式参数1>,<类型2><形式参数2>,… )
                            {<函数体>}
                        

                        体现在代码里面就是

                        int func(int n){
                            ...
                            return func(n - 1) + func(n - 2) + func(n - 3);
                        }
                        
                    • 0
                      @ 2023-3-4 19:14:37
                      #include <bits/stdc++.h>
                      using namespace std;
                      int main(){
                          int a[114514], n; cin >> n; a[1] = 1; a[2] = 1; a[3] = 1; //熟悉的棍子战术
                          for (int i = 4; i <= n; i ++) a[i] = a[i - 1] + a[i - 2] + a[i - 3];
                          cout << a[n]; return 0; //老子就不用递归!
                      }
                      

                      你干嘛,哈哈哎呦 嗨嗨嗨,老子就是用递推做!你管不着!

                      • 0
                        @ 2023-3-4 17:18:11

                        AC供大家参考

                        #include <iostream>//hetao3097453
                        using namespace std;
                        int fib(int n)
                        {
                            if(n == 1)
                            {
                                return 1;
                            }
                            else if(n == 2)
                            {
                                return 1;
                            }
                            else if(n == 3)
                            {
                                return 1;
                            }
                            else
                            {
                                return fib(n - 1) + fib(n - 2) + fib(n - 3);
                            }
                        }
                        int main()
                        {
                            int n;
                            cin >> n;
                            cout << fib(n) << endl;
                            return 0;
                        }
                        
                        • 0
                          @ 2023-3-4 17:10:21
                          #include<iostream>
                          using namespace std;
                          int n;
                          int func(int i)
                          {
                              if (i == 1) return 1;
                              if (i == 2) return 1;
                              if (i == 3) return 1;//和斐波那契数列有一点点像(明知道没用但为了题解不被删还是打上了的思路ヽ(ー_ー)ノ)}
                              return func(i-1) +func(i-2)+func(i-3);
                          }
                          int main()
                          {
                              cin >> n;
                              cout << func(n);
                          }
                          • 0
                            @ 2023-3-4 17:06:59
                            #include<iostream>
                            using namespace std;
                            int n;
                            int func(int i)
                            {
                                if (i == 1) return 1;
                                if (i == 2) return 1;
                                if (i == 3) return 1;
                                return func(i-1) +func(i-2)+func(i-3);
                            }
                            int main()
                            {
                                cin >> n;
                                cout << func(n);
                            }
                            老师骗人(#`皿′) 
                            明明没变好这(```ccp)白打了
                            • 0
                              @ 2023-2-28 15:01:34
                              思路 这道题的难度在于找到数列规律。 $\\$(1)不难发现从数列的第4项开始,每一项都等于它的前3项和,所以得到递归表达式: $\\f(n)=f(n-1)+f(n-2)+f(n-3)$ $\\$(2)那么终止条件也就是显然的,前3项都无法向前查找3项,都初始化为1.
                              代码
                              
                              #include<iostream>
                              using namespace std;
                              int n;
                              int func(int x)
                              {
                                  if (x <= 3) return 1;
                                  return func(x - 1) + func(x - 2) + func(x - 3);
                              }
                              int main()
                              {
                                  cin >> n;
                                  cout << func(n);
                              }
                              
                              
                              • -1
                                @ 2024-6-9 10:46:45
                                #include<bits/stdc++.h>
                                using namespace std;
                                int n;
                                int a(int x)
                                {
                                    if (x==1)  
                                        return 1;
                                    if (x==2)  
                                        return 1;
                                    if (x==3)  
                                        return 1;
                                    return a(x-1)+a(x-2)+a(x-3);
                                }
                                int main()
                                {
                                    cin>>n;
                                    cout<<a(n);
                                    return 0;
                                }
                                
                                • -1
                                  @ 2024-5-31 21:17:00

                                  ~~#include <iostream>//hetao3097453 using namespace std; int fib(int n) { if(n == 1) { return 1; } else if(n == 2) { return 1; } else if(n == 3) { return 1; } else { return fib(n - 1) + fib(n - 2) + fib(n - 3); } } int main() { int n; cin >> n; cout << fib(n) << endl; return 0; } ~~

                                  信息

                                  ID
                                  5
                                  时间
                                  1000ms
                                  内存
                                  256MiB
                                  难度
                                  5
                                  标签
                                  递交数
                                  6686
                                  已通过
                                  2803
                                  上传者