Created
August 9, 2016 13:58
-
-
Save kYroL01/eeb7752ae71f6ae58a095ad74e989986 to your computer and use it in GitHub Desktop.
Solution for game "The Descent" on CodingGame
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
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
/** | |
* The while loop represents the game. | |
* Each iteration represents a turn of the game | |
* where you are given inputs (the heights of the mountains) | |
* and where you have to print an output (the index of the moutain to fire on) | |
* The inputs you are given are automatically updated according to your last actions. | |
**/ | |
int main() | |
{ | |
int max_height, idx_height; | |
// game loop | |
while (1) { | |
max_height = 0; | |
idx_height = 0; | |
for (int i = 0; i < 8; i++) { | |
int mountainH; // represents the height of one mountain. | |
scanf("%d", &mountainH); | |
if(mountainH > max_height) { | |
max_height = mountainH; | |
idx_height = i; | |
} | |
} | |
// Write an action using printf(). DON'T FORGET THE TRAILING \n | |
// To debug: fprintf(stderr, "Debug messages...\n"); | |
printf("%d\n", idx_height); // The index of the mountain to fire on. | |
} | |
return 0; | |
} |
Thank you so much :)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* The while loop represents the game.
* Each iteration represents a turn of the game
* where you are given inputs (the heights of the mountains)
* and where you have to print an output (the index of the moutain to fire on)
* The inputs you are given are automatically updated according to your last actions.
**/
int main()
{
int max_height, idx_height;
// game loop
int mountainH[8] = {0};
while (1) {
for (int i = 0; i < 8; i++) {
scanf("%d", &mountainH[i]);
}
max_height = 0;
idx_height = 0;
for (int i = 0; i < 8; i++) {
if (mountainH[i] > max_height){
max_height = mountainH[i];
idx_height = i;
}
}
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
printf("%d\n", idx_height); // The index of the mountain to fire on.
}
return 0;
}
Hi @engrhafizmaqsood
I think your version is a bit inefficient because you're using 2 for
inside the while(1)
.
Basically is replicated code. You can do the same in one loop :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice code sir, thansk !!