I’m trying Shell Game . Since it’s from before December 2020, it requires the input to be read from a file and outputted to a file. My question is about this, but I am providing more of the code to show how I used it. (This is in Java):
File inputFile = new File("shell.in");
Scanner reader = new Scanner(inputFile);
FileWriter writer = new FileWriter("shell.out");
int n = reader.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
inputs[i][j] = reader.nextInt();
}
}
...
writer.write(Math.max(Math.max(shell1, shell2), shell3));
writer.close();
reader.close();
I already tried the method on the usaco guide, and it did not work, so I tried this way. It doesn’t output anything to the file, so I wanted to know what I was doing wrong. Thank you in advance!
Benq
November 26, 2022, 9:51pm
#2
Can you include all of your code (as mentioned in How to ask for help on a problem - #2 )?
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileWriter;
class Main {
public static void main(String[] args) throws IOException {
File inputFile = new File("shell.in");
Scanner reader = new Scanner(inputFile);
FileWriter writer = new FileWriter("shell.out");
int n = reader.nextInt();
int[][] inputs = new int[n][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
inputs[i][j] = reader.nextInt();
}
}
int[] shells = {1, 2, 3};
int shell1 = 0;
int shell2 = 0;
int shell3 = 0;
for (int k = 0; k < n; k++) {
shells = swap(inputs[k][0] - 1, inputs[k][1] - 1, shells);
int guess = inputs[k][2];
if (shells[guess - 1] == 1) {
shell1++;
} else if (shells[guess - 1] == 2) {
shell2++;
} else {
shell3++;
}
}
writer.write(Math.max(Math.max(shell1, shell2), shell3));
writer.close();
reader.close();
}
public static int[] swap(int index1, int index2, int[] nums) {
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
return nums;
}
}
Benq
November 27, 2022, 12:36am
#4
Switching to PrintWriter
and using print
instead of write
works:
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
class Main {
public static void main(String[] args) throws IOException {
File inputFile = new File("shell.in");
Scanner reader = new Scanner(inputFile);
PrintWriter writer = new PrintWriter("shell.out");
int n = reader.nextInt();
int[][] inputs = new int[n][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
inputs[i][j] = reader.nextInt();
}
}
int[] shells = {1, 2, 3};
int shell1 = 0;
int shell2 = 0;
int shell3 = 0;
for (int k = 0; k < n; k++) {
shells = swap(inputs[k][0] - 1, inputs[k][1] - 1, shells);
int guess = inputs[k][2];
if (shells[guess - 1] == 1) {
shell1++;
} else if (shells[guess - 1] == 2) {
shell2++;
} else {
shell3++;
}
}
writer.print(Math.max(Math.max(shell1, shell2), shell3));
writer.close();
reader.close();
}
public static int[] swap(int index1, int index2, int[] nums) {
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
return nums;
}
}
The method in USACO Guide is correct as far as I know (if not, let us know which specific part did not work).