航电 2041 超级楼梯
生活随笔
收集整理的這篇文章主要介紹了
航电 2041 超级楼梯
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
超級(jí)樓梯
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47105????Accepted Submission(s): 24020
Problem Description 有一樓梯共M級(jí),剛開始時(shí)你在第一級(jí),若每次只能跨上一級(jí)或二級(jí),要走上第M級(jí),共有多少種走法?
Input 輸入數(shù)據(jù)首先包含一個(gè)整數(shù)N,表示測(cè)試實(shí)例的個(gè)數(shù),然后是N行數(shù)據(jù),每行包含一個(gè)整數(shù)M(1<=M<=40),表示樓梯的級(jí)數(shù)。
Output 對(duì)于每個(gè)測(cè)試實(shí)例,請(qǐng)輸出不同走法的數(shù)量
Sample Input 2 2 3
Sample Output 1 2
Author lcy
Source 2005實(shí)驗(yàn)班短學(xué)期考試 ?利用遞推關(guān)系,就是Fibonacci數(shù)列,面對(duì)第n級(jí)臺(tái)階,可以選擇一步跨上,也可以跨兩步上去,所以對(duì)于一步的是f(n - 1),對(duì)于兩步的是f(n - 2),所以得到遞推關(guān)系是f(n) = f(n - 1) + f(n - 2) #include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> #define MAX_N 205 using namespace std;int ans[MAX_N];void pre() {ans[1] = 1;ans[2] = 2;for (int i = 3; i < 50; i++) {ans[i] = ans[i - 1] + ans[i - 2];} }int main() {int t, n;pre();scanf("%d", &t);while (t--) {scanf("%d", &n);printf("%d\n", ans[n - 1]);}return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/cniwoq/p/6770948.html
總結(jié)
以上是生活随笔為你收集整理的航电 2041 超级楼梯的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)linux运行tomcat时JRE
- 下一篇: JAVA Fork Join Demo