34 条题解

  • 61
    @ 2023-3-11 11:39:01

    AC代码

    难度:简单,代码和思路在下面,仅供参考~

    Difficulty:easy,code and ideas are here,for reference only~

    ------>>>目录<<<------

    • 思路
    • AC代码
    • 终极彩蛋(如何做弹窗)

    1.思路

    可能是看不懂图片,俺翻译一下~

    image

    n = 0时 返回1

    n = 1时 返回2 * x

    n > 1时 返回2 * x * h(n-1,x) - 2 * (n - 1) * h(n-2,x)

    2.AC代码

    #include <iostream>
    using namespace std;
    int h(int n,int x)//不要忘记加int
    {
        if (n == 0)
        {
            return 1;
        }
        else if (n == 1)
        {
            return 2 * x;
        }
        else if (n > 1)
        {
            return 2*x* h(n - 1,x) - 2*(n-1)* h(n-2,x);
        }
    }
    int main()
    {
        int x,n;
        cin >> n >> x;
        cout << h(n,x);
    }
    

    3.彩蛋

    注:俺在讨论里又写了一个讨论关于弹窗~

    image

    编码不易

    点赞走起

    记得点赞再抱走哦~

    The encoding is not easy

    you can support me

    remember to praise and refer to it~

    • @ 2023-7-29 22:58:36

      这题可以用记忆化来做,而且return已经终止到了上一层,不用else了 完整代码如下:

      
      
      #include<bits/stdc++.h>
      using namespace std;
      int a[100005];
      int h(int n,int x){
      if (n==0) return 1;
      if (n==1) return 2*x;
      if (a[n]) return a[n];
      a[n]=2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
      return a[n];
      }
      int main(){
      int n,x;
      cin>>n>>x;
      cout<<h(n,x);
      return 0;
      }
      
      
    • @ 2023-11-25 22:12:35

      @ 赞同

    • @ 2024-3-1 22:11:31

      👍

    • @ 2024-3-2 15:50:11

      翻译好评!

  • 5
    @ 2023-8-4 19:31:56

    普通思路:递归

    #include <bits/stdc++.h>
    using namespace std;
    int h(int n, int x)
    {
        if(n == 0)
            return 1;
        else if (n == 1)
            return 2 * x;
        else
            return 2 * x * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x);
    }
    int main()
    {
        int n, x;
        cin >> n >> x;
        cout << h(n, x);
        return 0;
    }
    

    稍微简化的思路:递归 + 记忆化搜索

    #include <bits/stdc++.h>
    using namespace std;
    int a[100005];
    int h(int n, int x)
    {
        if (n == 0)
            return 1;
        if (n == 1)
            return 2 * x;
        if (a[n])
            return a[n];
        a[n] = 2 * n * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x);
        //通过定义一个数组,实现记忆化搜索,减少时间复杂度
    }
    int main()
    {
        int n, x;
        cin >> n >> x;
        cout << h(n, x);
        return 0;
    }
    

    已AC,请放心食用,记得点赞再抱走哟🎉️

    • 3
      @ 2023-2-28 15:19:45
      提示
              本题的递归函数需要有两个参数。

      完整思路
              定义递归函数h(n,x),根据题目给定的规则,实现函数即可。

      代码
      #include <iostream>
      
      using namespace std;
      int h(int n, int x) { if (n == 0) return 1; if (n == 1) return 2 * x; return 2 * x * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x); }
      int main() { int n, x;
      cin >> n >> x; cout << h(n, x);
      return 0; }



      • 2
        @ 2024-3-9 11:40:24

        难度:简单

        你们应该都是看不懂图片吧?不知道那个下标n是怎么搞的吧?我来给你们翻译一下:

        意思就是说 n=0 时,返回 1 ; n=1 时,返回 2x ; n>1 时,返回 2xh(n-1,x)-2(n-1)*h(n-2,x) (其实n>1时的返回代码直接写就行了,不用什么ifelseif else的) 说到这,大家都会了吧? 下面是AC代码,安心逝用即可:

        #include<bits/stdc++.h>
        using namespace std;
        int n,x;
        int h(int n,int x){
            if(n==0){
                return 1;
            }
            else if(n==1){
                return 2*x;
            }
            return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
        }
        int main(){
            cin>>n>>x;
            cout<<h(n,x);
            return 0;
        }
        
        • 1
          @ 2023-10-15 14:55:49
          #include <bits/stdc++.h>
          using namespace std;
          int h(int n,int x){//因为是形参, 所以不影响。 
          	if(n==0){//按题目上面做就行了。(学到这也不要我多讲吧) 
          		return 1; 
          	}else if(n==1){
          		return 2*x;
          	}else if(n>1){
          		return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);//这个自己打,小心点。 
          	}
          }
          int main(){
          	int n,x;//因为题目说了(n<=8,x<=8) ,所以直接用int。
          	cin>>n>>x;
          	cout<<h(n,x);
          }
          
          • 1
            @ 2023-8-4 20:59:20
            #include <bits/stdc++.h>
            using namespace std;
            int h( int n , int x )
            {
            if ( n == 0 )
            {
            return 1;
            }
            else if ( n == 1 )
            {
            return 2 * x;
            }
            else if ( n > 1 )
            {
            return 2 * x * h( n - 1 , x ) - 2 * ( n - 1 )* h( n - 2 , x );
            }
            }
            int main()
            {
            int x , n;
            cin >> n >> x;
            cout << h( n , x );
            }
            
            • 0
              @ 2023-12-8 19:21:20
              #include <iostream>
              using namespace std;
              int h(int n,int x)
              {
                  if (n == 0)
                  {
                      return 1;
                  }
                  else if (n == 1)
                  {
                      return 2 * x;
                  }
                  else if (n > 1)
                  {
                      return 2*x* h(n - 1,x) - 2*(n-1)* h(n-2,x);
                  }
              }
              int main()
              {
                  int x,n;
                  cin >> n >> x;
                  cout << h(n,x);
                  return 0;
              }
              
              
              • 0
                @ 2023-10-14 19:05:37

                整体简单,以下是函数代码(你直接写主体里也没问题)

                int h(int n,int x)
                {
                    if(n==0)
                    {
                        return 1;
                    }
                    if(n==1)
                    {
                        return 2*x;
                    }
                    return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
                }
                
                • 0
                  @ 2023-10-13 21:49:01

                  C2每课一题解(第二课 第二题)!!!

                  此题简单,只需用上节课的知识——递归

                  话不多说,上代码!

                  AC Code

                  #include <iostream>
                  int a[10]; 
                  int h(int n,int x)
                  {
                      if(n==0) //第1种情况。
                      {
                          return 1;
                      }
                      if(n==1)//第2种情况。
                      {
                          return 2*x;
                      }
                      if(a[n])
                      {
                          return a[n];
                      }
                      return a[n]=2*x*h(n-1,x)-2*(n-1)*h(n-2,x);//最终情况,注意不要写错。
                  }
                  
                  • 0
                    @ 2023-8-21 8:56:16

                    使用递归

                    #include<bits/stdc++.h>
                    using namespace std;
                    int h(int n, int x){
                        if (n == 0){
                            return 1;
                        }
                        else if (n == 1){
                            return 2 * x;
                        }
                        else{
                             return 2* x * h(n-1,x) - 2*(n-1) * h(n-2,x) ;
                        }
                    }
                    int main()
                    {
                        int f,k;
                        cin >> f >> k;
                        cout << h(f,k);
                        return 0;
                    }
                    
                    • 0
                      @ 2023-8-5 21:56:14
                      //整个人都无语住了,一坨子东西最开始愣是没看懂[无语]
                      #include <bits/stdc++.h>//题解由hetao1098709提供,禁止抄袭
                      using namespace std;
                      int zhenwuyu(int n,int x)
                      {
                          if(n==0)//如果等于0
                          {
                              return 1;//返回1
                          }
                          if(n==1)//如果等于1
                          {
                              return 2*x;//返回x的2倍
                          }
                          if(n>1)//如果n>1
                          {
                              return 2*x*zhenwuyu(n-1,x)-2*(n-1)*zhenwuyu(n-2,x);//返回这一坨子,不要抄错了,这一坨子可以复制粘贴。
                          }
                      }
                      int main()
                      {
                          int n,x;
                          cin>>n>>x;
                          cout<<zhenwuyu(n,x);//这些不用我解释了吧,都学到这了。
                      }
                      
                      • @ 2023-8-5 21:56:46

                        编码不易,点赞后在写吧。

                    • 0
                      @ 2023-8-4 21:48:10
                      #include <cstdio>
                      using namespace std;
                      int h(int n,int x){
                          if(n==0)return 1;
                          if(n==1)return 2*x;
                          if(n>1)return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
                      }
                      int main(){
                          int n,x;
                          scanf("%d%d",&n,&x);
                          printf("%d\n",h(n,x));
                          return 0;
                      }
                      
                      • 0
                        @ 2023-8-4 18:52:19

                        hihihi雾来~~~

                        
                        

                        #include <iostream> using namespace std; int h(int n,int x) { if (n == 0) { return 1; } else if (n == 1) { return 2 * x; } else if (n > 1) { return 2x h(n - 1,x) - 2*(n-1)* h(n-2,x); } } int main() { int x,n; cin >> n >> x; cout << h(n,x); }

                        
                        
                        • 0
                          @ 2023-7-31 21:14:46

                          yasuo👀️

                          #include <iostream>
                          int book[10]; 
                          int h(int n,int x){
                              if(n==0) return 1;
                              if(n==1) return 2*x;
                              if(book[n]) return book[n];
                              return book[n]=2*x*h(n-1,x)-2*(n-1)*h(n-2,x);}
                          int main(){
                              int n,x;std::cin>>n>>x;
                              std::cout<<h(n,x);return 0;}
                          
                          • 0
                            @ 2023-7-29 22:59:45

                            这题可以用记忆化来做 完整代码如下:

                            #include<bits/stdc++.h>
                            using namespace std;
                            int a[100005];
                            int h(int n,int x){
                                if (n==0) return 1;
                                if (n==1) return 2*x;
                                if (a[n]) return a[n];
                                a[n]=2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
                                return a[n];
                            }
                            int main(){
                                int n,x;
                                cin>>n>>x;
                                cout<<h(n,x);
                                return 0;
                            }
                            
                            
                            
                            • 0
                              @ 2023-7-25 15:17:21

                              -题目回顾-

                              输入两个数n和x,若n为0时返回1;n为1时返回2倍x;n > 1时返回2 * x * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x)

                              这里可以编写一个函数h(n, x)把三种情况的返回值写进函数里即可!

                              #include <bits/stdc++.h>//by AGOMG
                              using namespace std;
                              int h(int n, int x)
                              {
                                  if(n == 0)
                                      return 1;
                                  else if(n == 1)
                                      return 2 * x;
                                  else if(n > 1)
                                      return 2 * x * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x); 
                              }//核心部分
                              
                              • 0
                                @ 2023-5-28 19:00:12

                                #include<bits/stdc++.h> using namespace std; unsigned n,x; int hn(int a,int b) { if(a0) return 1; if(a1) return 2b; return 2bhn(a-1,b)-2(a-1)*hn(a-2,b); }

                                int main() { cin>>n>>x; cout<<hn(n,x); }

                                • 0
                                  @ 2023-5-20 19:39:47

                                  思路:照着题目写就行 代码如下:

                                  #include <iostream>
                                  using namespace std;
                                  int h(int n,int x)
                                  {
                                  	if(n==0) return 1;
                                  	else if(n==1) return x*2;
                                  	else if(n>1) return 2*x* h(n - 1,x) - 2*(n-1)* h(n-2,x);
                                  }
                                  int main()
                                  {
                                      int x,n;
                                      cin>>n>>x;
                                      cout<<h(n,x);
                                  }
                                  

                                  点赞再抱走哦~

                                  • 0
                                    @ 2023-5-14 14:45:21

                                    首先读懂题

                                    image

                                    用我们平时常用的函数表示一下就是

                                    h(n,x)={

                                    1,n==0

                                    2x,n==1

                                    2x·h(n-1,x)-2(n-1)·h(n-2,x),n>1

                                    }

                                    所以按照它编写程序就行啦

                                    核心函数代码

                                    int h(int n,int x){
                                        if(n==0)return 1;
                                        if(n==1)return 2*x;
                                        return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
                                    }
                                    

                                    当然也可以用递推,开一个二维数组再用两个嵌套的循环就可以了

                                    • -1
                                      @ 2024-5-4 20:39:57
                                      #include<bits/stdc++.h>
                                      using namespace std;
                                      int h(int n ,int x){
                                          if(n==0){
                                              return 1;
                                          }
                                          else if(n==1){
                                              return x*2;
                                          }
                                          return 2*x*h(n-1,x)-2*(n-1)*h(n-2,x);
                                          
                                      }
                                      int main(){
                                          int n,x;
                                          cin>>n>>x;
                                          cout<<h(n,x);
                                      }//已AC,请放心食用
                                      

                                      信息

                                      ID
                                      9
                                      时间
                                      1000ms
                                      内存
                                      256MiB
                                      难度
                                      4
                                      标签
                                      递交数
                                      4262
                                      已通过
                                      2003
                                      上传者