Game keeps telling me that int max and int imax aren’t initialised but I don’t know why… Any ideas?
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (true) {
int max;
int iPosition;
for (int i = 0; i < 8; i++) {
int mountainH = in.nextInt(); // represents the height of one mountain.
if(max < mountainH){
max = mountainH;
iPosition = i;
}
}
System.out.println(iPosition); // The index of the mountain to fire on.
}
}
Hello everyone, I didn’t understand but, this ship is shooting ‘mountain 7’ two times. The first shot destroys the mountain, so why does it shoot again?
while (true) {
let arr = [];
for (let i = 0; i < 8; i++) {
const mountainH = parseInt(readline());
arr.push(mountainH);
}
let max=0;
for(let i=0; i<8; i++) {
if(max<arr[i]){
max=arr[i];
}
}
for(let i=0; i<8; i++){
if(max==arr[i]){
console.log(String(i));
}
}
}
Because there may be mountains of the same height.
In the last part of your while loop, you print the index of the mountain to shoot whenever the height is equal to the maximum height, and there can be more than one of those.
Thank you for your answer.
There is one mountain at height of 7. The ship shoots it and it got destroyed. Then It supposed to choose another mountain. There is two mountains at height of 5. But the ship shoots that mountain again. Its height is zero. Why? I don’t get it.
The ship shoots according to the output of your code. If your code instructs it to shoot twice in the same turn, or to shoot the same mountain in the following turn, the ship does so accordingly. To correct the ship’s behaviour, you have to correct your code.
Please help me ! Code
int main()
{
while (1) {
for (int i = 0; i < 8; i++) {
int mountain_h;
mountain_h = scanf("%d", &mountain_h);
printf("%d", mountain_h);
}
}
return 0;
}
Just wanted to give someone, like me, a heads up about the solution for this one in javascript.
Use let instead of var - must be something with the JS versions being old/new
[GT→ Your biggest problem is that your previous loop has nothing to do with the subsequent printing. If you delete “print(“0”)”, all the previous codes may also be able to pass the first level. It’s because your code is for the first level. The following code can only help you pass the first or second level. I really don’t know how to store the number of the mountains after being hit, and I don’t know the source. Is there any change in the value of the mountain after printing the subscript in the code, so I can only pass the second level.]
a = []
i = 0
while True:
while i<8:
mountain_h = int(input()) # represents the height of one mountain.
a.append(mountain_h)
i += 1
# 01 02
for x in range(8):
print(a.index(max(a)))
a[a.index(max(a))] = 0
[GT→ That is, after hitting the mountain every time, the cycle is repeated again. At this time, the 8 inputs printed by the machine will be updated, and the updated value corresponds to the value on the picture in real time. Understand this, the problem is not difficult to solve]
Hi ! Please someone could explain me why the code below does not work ? I can’t understand why.
The first lap, it destroyed the good montain. Next, during the second lap, a missile destroyed me ^^
while (true) {
let max = 0;
let indexMax = 0;
for (let i = 0; i < 8; i++) {
const mountainH = parseInt(readline()); // represents the height of one mountain.
if (mountainH > max ) {
max = mountainH;
indexMax = i;
}
}
console.log(indexMax);
break; // The index of the mountain to fire on.
}
Hello there, just finished up the Defibrillators puzzle, and looking to continue with the Descent, but the Tab and Ctrl keys seems to be ineffective now… like, they’re still working on my computer, but they don’t have any effect on the computer integrated IDE…
Anyone experienced that issue before ?
EDIT: Seems like I was in EMACS configuration instead of Classic in the parameters, no idea how it got like that, but anyway, problem seems to be solved
import sys
import math
# 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 mountain to fire on)
# The inputs you are given are automatically updated according to your last actions.
mts = []
# game loop
while True:
for i in range(8):
mountain_h = int(input()) # represents the height of one mountain.
mts.append(mountain_h)
mts.sort()
print(len(mts) - 1)
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
# The index of the mountain to fire on.
I don’t understand why this would not work. The largest mountain is immediately sorted to the end and printed.