涉及到乘法,可能會用到long long int!否則WA!-- D - 一隻小蜜蜂...
阿新 • • 發佈:2021-02-16
技術標籤:演算法技巧
https://vjudge.net/contest/423078#rank
有一隻經過訓練的蜜蜂只能爬向右側相鄰的蜂房,不能反向爬行。請程式設計計算蜜蜂從蜂房a爬到蜂房b的可能路線數。
其中,蜂房的結構如下所示。
Input
輸入資料的第一行是一個整數N,表示測試例項的個數,然後是N 行資料,每行包含兩個整數a和b(0<a<b<50)。
Output
對於每個測試例項,請輸出蜜蜂從蜂房a爬到蜂房b的可能路線數,每個例項的輸出佔一行。
Sample Input
2 1 2 3 6
Sample Output
1 3
#include<cstdio> #include<iostream> #pragma warning (disable:4996) #include<string> using namespace std; const int maxn = 50 + 10; long long int ans[maxn] = { 0 }; void init() { ans[1] = 1L; ans[2] = 1L; for (int i = 3; i < maxn; i++) ans[i] = ans[i - 1] + ans[i - 2]; } long long int calcu(int x, int y) { int tmp = x - 1; y = y - tmp; return ans[y]; } int main() { init(); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); printf("%lld\n", calcu(x, y)); } return 0; }