94 条题解
-
-1
st=>start: Start:>[https://www.zybuluo.com (https://www.zybuluo.com/) io=>inputoutput: verification op=>operation: Your Operation cond=>condition: Yes or No? sub=>subroutine: Your Subroutine e=>end st->io->op->cond cond(yes)->e cond(no) ->sub->io
-
-1
$E=mc2$ st=>start: Start:>[https://www.zybuluo.com (https://www.zybuluo.com/) io=>inputoutput: verification op=>operation: Your Operation cond=>condition: Yes or No? sub=>subroutine: Your Subroutine e=>end st->io->op->cond cond(yes)->e cond(no) ->sub->io
-
-1
st=>start: Start:>https://www.zybuluo.com io=>inputoutput: verification op=>operation: Your Operation cond=>condition: Yes or No? sub=>subroutine: Your Subroutine e=>end
st->io->op->cond cond(yes)->e cond(no)->sub->io
-
-1
$E=mc2 st=>start: Start:>https://www.zybuluo.com io=>inputoutput: verification op=>operation: Your Operation cond=>condition: Yes or No? sub=>subroutine: Your Subroutine e=>end
st->io->op->cond cond(yes)->e cond(no)->sub->io
-
-1
$E=mc2 st=>start: Start:>https://www.zybuluo.com io=>inputoutput: verification op=>operation: Your Operation cond=>condition: Yes or No? sub=>subroutine: Your Subroutine e=>end
st->io->op->cond cond(yes)->e cond(no)->sub->io
-
-1
哈哈哈第一次发题解有点紧张,如果哪里有错请大家指出! #include<bits/stdc++.h> using namespace std; 直接万能头文件 int hetao(int n) 定义函数 { if(n==1) { return 1; } if(n==2) { return 1; } if(n==3) { return 1; 前三个全部返回1 } return hetao(n-1)+hetao(n-2)+hetao(n-3); } 这里因为规律是前三个数加起来的和, 所以要这样写 int main() { int n; 主函数就是直接 cin>>n; 定义输入加运算三步走 cout<<hetao(n); } 哈哈哈这就是我的见解哪里不妥请各位帅哥美女们提出xiè xiè la!
-
-3
OK呀,这道题
原石也是废常的简单好吧 首先,由题可知前三个数都等于1,所以我们要这样if (i == 1 || i == 2 || i == 3)//前三个返回1 { return 1; }
然后我们又由题可知,从第四个数开始都等于前三个数相加(如:3=1+1+1,5=3+1+1) 所以得出以下代码
return func(i-1)+func(i-2)+func(i-3);
最后,废话不多说,上完整代码
#include <bits/stdc++.h>//万能头文件,启动! using namespace std;//不要复制 int func(int i)//不要复制 { if (i == 1 || i == 2 || i == 3)//前三个返回1 { return 1;//不要复制 } return func(i-1)+func(i-2)+func(i-3);//不要复制 } int main()//不要复制 { long long i;//别问我为什么用longlong cin >> i;//不要复制 cout << func(i);//不要复制 return 0;//不要复制 }
-
-3
这是记忆化搜索的代码(稍快)
#include <iostream> using namespace std; int n,a[35]; int func(int m) { if(m==1||m==2||m==3) return 1; else if(a[m]) return a[m]; else a[m]=func(m-1)+func(m-2)+func(m-3); return a[m]; } int main() { cin >> n; cout << func(n); return 0; } //本人推荐
下面是普通代码(程序稍简单)
#include <iostream> using namespace std; int n,a[35]; int func(int m) { if(m==1||m==2||m==3) return 1; else return func(m-1)+func(m-2)+func(m-3); } int main() { cin >> n; cout << func(n); return 0; }
-
-3
P1001 找规律
题目描述
有一列数的排列是如下1,1,1,3,5,9,17,31,57,105,……1,1,1,3,5,9,17,31,57,105,……依次类推下去,请你找到规律并求出这个数列第n项。
第一部分 递归求解
首先,规律这方面很简单,即为
fib(n) = fib(n - 1) + fib(n - 2) + fib(n - 3)
找到了规律,再来看终止条件:
1 = 1;2 = 1;3 = 1;(三个基础的对吧)
(终止条件这部分我用的是if-else if)
再加上输入输出,大功告成!!!
代码:
#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; }
29行,去缩进大概20行搞定
第二部分 递推求解
同样找规律,不过代码简单得多,大概有缩进10多行
时间紧,这里就不放代码了
有兴趣可以自行补充
hetao3097453(bililili @ 一钩出站)
2023年3月3日
信息
- ID
- 5
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 6686
- 已通过
- 2803
- 上传者