34 条题解
-
61
AC代码
难度:简单,代码和思路在下面,仅供参考~
Difficulty:easy,code and ideas are here,for reference only~
------>>>目录<<<------
- 思路
- AC代码
-
终极彩蛋(如何做弹窗)
1.思路
可能是看不懂图片,俺翻译一下~
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.彩蛋
注:俺在讨论里又写了一个讨论关于弹窗~
编码不易
点赞走起
记得点赞再抱走哦~
The encoding is not easy
you can support me
remember to praise and refer to it~
-
5
普通思路:递归
#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
提示
本题的递归函数需要有两个参数。
完整思路
定义递归函数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
难度:简单
你们应该都是看不懂图片吧?不知道那个下标n是怎么搞的吧?我来给你们翻译一下:
意思就是说 n=0 时,返回 1 ; n=1 时,返回 2x ; n>1 时,返回 2xh(n-1,x)-2(n-1)*h(n-2,x) (其实n>1时的返回代码直接写就行了,不用什么
if
、else
、if 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
#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
#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
//整个人都无语住了,一坨子东西最开始愣是没看懂[无语] #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);//这些不用我解释了吧,都学到这了。 }
-
0
-题目回顾-
输入两个数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); }//核心部分
信息
- ID
- 9
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 4262
- 已通过
- 2003
- 上传者