Created
December 17, 2014 15:27
-
-
Save sicknarlo/ceb88395ed5b2fbf4b43 to your computer and use it in GitHub Desktop.
[HackerRank] Utopian Tree
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
/* The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, | |
when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. | |
Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the | |
height of the tree after N growth cycles? */ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int getCase(); | |
int height(int N); | |
//Takes the number of cycles and calculates the trees height | |
int main(){ | |
int T = 0; | |
cin >> T; | |
vector<int> results(T); | |
int caseCount = 0; | |
//get all of the cases and put into array | |
for (int i = 0; i < T; i++){ | |
int j; | |
j = getCase(); | |
results[i] = j; | |
caseCount++; | |
} | |
for (int i = 0; i < caseCount; i++){ | |
int h = height(results[i]); | |
cout << h << endl; | |
} | |
//calculate and print height for each case | |
system("pause"); | |
return 0; | |
} | |
int getCase(){ | |
int N; | |
cin >> N; | |
return N; | |
} | |
int height(int N){ | |
int H(1); | |
int count = 1; | |
if (N== 0) | |
return H; | |
else { | |
while (count <= N){ | |
if (count % 2 != 0){ | |
H = H*2; | |
count++; | |
} | |
else{ | |
H++; | |
count++; | |
} | |
} | |
} | |
return H; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment