Skip to content

Instantly share code, notes, and snippets.

@sicknarlo
Created December 17, 2014 15:27
Show Gist options
  • Save sicknarlo/ceb88395ed5b2fbf4b43 to your computer and use it in GitHub Desktop.
Save sicknarlo/ceb88395ed5b2fbf4b43 to your computer and use it in GitHub Desktop.
[HackerRank] Utopian Tree
/* 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