-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeek-onacci Number.cpp
More file actions
67 lines (47 loc) · 1.26 KB
/
Copy pathGeek-onacci Number.cpp
File metadata and controls
67 lines (47 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Geek created a random series and given a name geek-onacci series.
Given four integers A, B, C, N. A, B, C represents the first three numbers of geek-onacci series.
Find the Nth number of the series. The nth number of geek-onacci series is a sum of the last
three numbers (summation of N-1th, N-2th, and N-3th geek-onacci numbers)
Input:
1. The first line of the input contains a single integer T denoting the number of test cases.
The description of T test cases follows.
2. The first line of each test case contains four space-separated integers A, B, C, and N.
Output: For each test case, print Nth geek-onacci number
Constraints:
1. 1 <= T <= 3
2. 1 <= A, B, C <= 100
3. 4 <= N <= 10
Example:
Input:
3
1 3 2 4
1 3 2 5
1 3 2 6
Output:
6
11
19
*/
#include<bits/stdc++.h>
using namespace std;
int solve(int a, int b, int c, int n) {
if (n == 1) return a;
else if (n == 2) return b;
else if (n == 3) return c;
int next = a + b + c;
return solve(b, c, next, n - 1);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int a, b, c, n;
cin >> a >> b >> c >> n;
cout << solve(a, b, c, n) << "\n";
}
return 0;
}