Created
September 10, 2017 07:40
-
-
Save zeuschild/ac45ba513a6a68ceb1613b8eed352c39 to your computer and use it in GitHub Desktop.
Oursky Developer Pre-Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write a function that takes an array of integers as input. For each integer, output the next | |
// fibonacci number. | |
// Fibonacci number of Fn is defined by: | |
// Fn = Fn-1 + Fn-2 | |
// F1 = 1, F2 = 1 | |
// Your program should run correctly for the first 60 Fibonacci Numbers. | |
// For example: | |
// nextFibonacci([1,9,22]) | |
// Output: | |
// 2 | |
// 13 | |
// 34 | |
// Explanation: 2 is the next fibonacci number great than 1. The fibonacci number after 9 is 13, | |
// and after 22 is 34. | |
void nextFibonacci(vector<int> &arr) { | |
vector<int> Fibonnacci; | |
Fibonnacci.push_back(1); | |
Fibonnacci.push_back(1); | |
while(true) { | |
if(arr.size()<=0) break; | |
int idx = Fibonnacci.size(); | |
Fibonnacci.push_back(Fibonnacci[idx-1] + Fibonnacci[idx-2]); | |
for(int i=0; i<arr.size(); i++) { | |
if (Fibonnacci[Fibonnacci.size()-1] > arr[i]) { | |
cout << Fibonnacci[Fibonnacci.size()-1] << endl; | |
arr.erase(arr.begin() + i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment