题目:
Problem Description
Fibonacci sequence is familiar to you, right? Here comes the SZ Fibonacci sequence, with the property like the following formula.
F(n)= a (n=1)
F(n)= b (n=2)
F(n)= F(n-1)+F(n-2), n>2 and n is odd
F(n)= F(n-1)+F(n-2)+F(n-3), n>2 and n is even
Here a and b are constants. Given a,b,and n, your task is to calculate the F(n).
F(n)= a (n=1)
F(n)= b (n=2)
F(n)= F(n-1)+F(n-2), n>2 and n is odd
F(n)= F(n-1)+F(n-2)+F(n-3), n>2 and n is even
Here a and b are constants. Given a,b,and n, your task is to calculate the F(n).
Input
First line of input comes a positive integer T(T<=10), indicating the number of test cases. Each test case contains three positive integer a,b and n(a<=10,b<=10,n<=30)
Output
Print one line containing an integer, i.e.F(n), for each test case.
Sample Input
2 1 2 3 1 3 6
Sample Output
3 24
测试数据第一行,是代表需要测试数据的组数,
第二行:第一个数是数列第一个数的值,第二个数是数列第二个数的值。第三个数是需要求数列的第几项。
当需要求的项为奇数时:所求为它的前一项的值+前两项的值;
当需要求的项为偶数时:嗦求为它的前一项的值+前两项的值+前三项的值;
例如:
1 2 3
即求第三项的值; 也就是第一项的值+第二项的值=1+2=3;
即为输出结果。
- # include<cstdio>
- # include<iostream>
- using namespace std;
- int a,b; //全局变量;
- int fun2(int n); //函数声明;
- int fun1(int n)
- {
- if(n==2) return b;
- if(n>2) return fun2(n-1)+fun1(n-2)+fun2(n-3);
- }
- int fun2(int n)
- {
- if(n==1) return a;
- if(n>2) return fun1(n-1)+fun2(n-2);
- }
- int main()
- {
- int n;
- cin>>n;
- while(n--)
- {
- int cnt=0;
- cin>>a>>b>>cnt;
- if(cnt%2==0) cout<<fun1(cnt)<<endl; //判断cnt的奇偶性;
- else cout<<fun2(cnt)<<endl;
- }
- return 0;
- }