1. 输入的第一行是一个整数n
2. 接下来n行数据,每行一组测试用例
3. 每组测试用例包含两个正整数a和b,(0 < a < b < 2^31)
每组用例的结果单独输出一行。输出数据结果范围是 [0, 2^63)。
3<br/>1 2<br/>3 6<br/>99 100
1<br/>3<br/>1
#include <iostream> #include <stdio.h> using namespace std; long long diff[103] = {1, 1, 2}; int main() { for(int i=3; i<103; i++) { diff[i] = diff[i-1] + diff[i-2]; } unsigned int n; unsigned int a, b, d; scanf("%d", &n); while(n--) { scanf("%d %d", &a, &b); d = b - a; printf("%lld\n", diff[d]); } return 0; }
详细题解:
https://blog.csdn.net/qq_33375598/article/details/104609103
设n为b与a的差值,F(n)为从a到b的方案。
由题目的图知,下面开始递推讨论
当n为3时:
当n为4时:
依次递推,得到递推公式:F(n) = F(n-1) + F(n-2).
也就是斐波那契数列。
由动态规划的知识:
递推式子:
递推边界:
从递推边界出发,依次递推便可得到整个递推式。
#include <cstdio> typedef long long ll; const int MAXN = 100010; ll f[MAXN] = {1,1,2}; int main(int argc, char const *argv[]){ int n, a, b; for (int i = 3; i < MAXN; ++i) { f[i] = f[i -1] + f[i -2];//递推式 } scanf("%d", &n); while(n--){ scanf("%d%d", &a, &b); printf("%lld\n", f[b - a]); } return 0; }
#include<iostream> #define ll long long using namespace std; const int maxn=100; ll fib[maxn]; int main() { fib[1]=1; fib[2]=2; for(int i=3; i<=95; i++) { fib[i]=fib[i-1]+fib[i-2]; } int T; int a,b; cin>>T; while(T--) { scanf("%d %d",&a,&b); printf("%lld\n",fib[b-a]); } return 0; }
思路就是可以前进一步或者两步和小孩跳楼梯一模一样。 #include <iostream> #include <vector> using namespace std; /* 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛? */ int main() { vector<long long> v(100); long long temp; v[0] = 0;// D v[1] = 1;// D A v[2] = 2;// D A B for (int i = 3; i < 100; i++) { v[i] = v[i - 1] + v[i - 2]; } int n; int a, b, c; while (cin >> n) { for (int i = 0; i < n; i++) { cin >> a >> b; c = b - a; cout << v[c] << endl; } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int t = scanner.nextInt(); for(int k = 0; k<t; k++){ int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println(getRes(x, y)); } } } private static long getRes(int x, int y) { int N = y-x+1; if(N<=2) return 1; long [] dp = new long[N+1]; dp[1] = 1; dp[2] = 1; for(int i = 3; i<=N; i++) dp[i] = (dp[i-2]+dp[i-1]); return dp[N]; } }java动规
解题思路:
由于排列规则可知,以及只能往左、右走,可知只有n-2、n - 1到达n。
那不就得出了f(n) = f(n - 1) + f(n - 2)么,这不就是斐波拉契尔数列问题么。。。
路线数 始、终差
1->1 1 0
1->2 1 1
1->3 2 2
1->4 3 3
1->5 5 4
1->6 8 5
1、1、2、3、5、8不就是一个斐波拉契尔数列么
代码实现:\color{blue}代码实现:代码实现:
#include <iostream> using namespace std; int main(int argc, const char * argv[]) { //建立一张表,用于记录f(n)各项值,注意数据溢出,使用long long型 //由于斐波拉契尔数列第103项已经超过了2^63,并且题目说明结果不会超过2^63,因此计算1-102 long long fTable[103] = {1, 1}; for (int i = 2; i < 103; ++i) { fTable[i] = fTable[i - 1] + fTable[i - 2]; } int count = 0; //scanf返回值为正确输入数据的变量个数,当一个变量都没有成功获取数据时,此时返回-1 while (scanf("%d", &count) != - 1) { for (int i = 0; i < count; ++i) { int a = 0, b = 0; scanf("%d %d", &a, &b); printf("%lld\n", fTable[b - a]); } } return 0; }
————————————————
版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://hestyle.blog.csdn.net/article/details/104629461
#include<iostream> #include<vector> using namespace std; //典型fb数列 int main() { long long a,b,n; vector<long long> dp(120,0); dp[0]=1; dp[1]=1; for (int i = 2; i < 120; i++) { dp[i]=dp[i-1]+dp[i-2]; } while (cin >>n) { for(int i=0;i<n;i++){ cin>>a>>b; cout << dp[b-a] << endl; } } }
#include<stdio.h> long fib(int n) { long pre = 1; long next = 0; long result = 1; while(n > 1) { n--; next = pre; pre = result; result = pre + next; } return result; } int main(void) { int n; scanf("%d", &n); while(n--) { int a; int b; scanf("%d%d", &a, &b); printf("%ld\n", fib(b - a)); } }
//搞错了没有,全是斐波拉契#include <stdio.h>void DataInit(long long *a);int main(){int N, i;long long a, b;long long A[100001] = {0, 1, 2};DataInit(A);scanf("%d", &N);for(i=0;i<N;i++){scanf("%lld %lld", &a, &b);printf("%lld\n", A[b-a]);}return 0;}void DataInit(long long *a){int i;for(i=3;i<100001;i++){a[i] = a[i-1] + a[i-2];}}
}
好像没人用动态存储。。
#include <stdio.h> #include <stdlib.h> int main() { int n; while (~scanf("%d", &n)) { short *s = (short*)calloc(n+1, sizeof(short)); int maxs=*s; for (int t = 1; t <= n; t++) { int a, b; scanf("%d%d", &a, &b); *(s + t) = b - a; if (*(s + t) > maxs) maxs = *(s + t); } long long *fib = (long long*)calloc(maxs+1, sizeof(long long)); *(fib+1) = 1; *(fib + 2) = 2; for (int i = 3; i <= maxs; i++) fib[i] = fib[i - 1] + fib[i - 2]; for (int i = 1; i <= n; i++) printf("%lld\n", fib[s[i]]); free(s);free(fib);s=NULL;fib=NULL; } return 0; }
#include<cstdio> using namespace std; long ans[103]; int n,a,b; int main(){ ans[1]=1; ans[2]=2; for(int i=3 ;i<103 ;i++){ ans[i] = ans[i-1] + ans[i-2]; } while(scanf("%d",&n)!=EOF){ while(n--){ scanf("%d%d",&a,&b); int x = b-a; printf("%ld\n",ans[x]); } } return 0; }
#include <stdio.h>
#include <iostream>
using namespace std ;
long fib[ 2000 ] = { 0 };
int main() {
fib [ 1 ] = 1 ;
fib [ 2 ] = 2 ;
for ( int i = 3 ; i < 2000 ; i++) {
fib [i] = fib [i- 1 ] + fib [i- 2 ];
}
int n,a,b;
scanf ( "%d" , &n);
while (n--) {
scanf ( "%d%d" , &a,&b);
printf ( "%ld\n" , fib [b-a]);
}
return 0 ;
}