Solving Aizu online spreadsheet problem.
The following code is a presentation error.
According to Aizu Online Judge issue (presentation error), it seems to mean that the output specification is violated.
However, at first glance, the results are the same as Sample Output
.
What's the difference?
Also, how do I find the difference between Sample Output
and extra spaces?
import java.util.Arrays;
import java.util.Scanner;
public class test {
public static void main(String[]args) {
Scanner scan = new Scanner (System.in);
intr=scan.nextInt();
intc=scan.nextInt();
int[][]matrix_rc = new int[r+1][c+1];
int sum_r;
int[]sum_c = new int[c+1];
Arrays.fill(sum_c,0);
for(inti=0;i<r;i++){
sum_r = 0; // Initialize on each line
for (int j = 0; j<c+1;j++) {
if(j!=c){
matrix_rc[i][j] = scan.nextInt();
sum_r+=matrix_rc[i][j];}
else if(j==c){
matrix_rc[i][j] = sum_r;
} //c+1 column completed
sum_c[j]+=matrix_rc[i][j]; // Replace array with all j columns
}
}
for (int j = 0; j<c+1;j++) {
matrix_rc[r][j] = sum_c[j];
} //r+1 array substitutes
for(inti=0;i<r+1;i++){
for (int j = 0; j<c+1;j++) {
System.out.print(matrix_rc[i][j]);
if(j<=c) {System.out.print("");}
}System.out.println();
}
scan.close();
}
}
There are sample inputs and outputs, so I think you should write a program to check if the output meets the input/output specifications.
public class TestTest{
public static void main(String args[]){
// input specification
String [ ] input = new String [ ] {
"4 5",
"1 1 3 4 5",
"2 2 2 4 5",
"3 3 0 1 1",
"2 3 4 4 6",
};
System.setIn(newByteArrayInputStream(String.join(System.lineSparator(), input).getBytes()));
// Retain output
PrintStream originalOut=System.out;
ByteArrayOutputStream dummyOut=new ByteArrayOutputStream();
System.setOut(new PrintStream(dummyOut));
// execution
test.main(null);
// output specification
String [ ] expect = new String [ ] {
"1 1 3 4 5 14",
"2 2 2 4 5 15",
"3 3 0 1 1 8",
"2 3 4 4 6 19",
"8 9 9 13 17 56"
};
System.setOut(originalOut);
String[]actual=dummyOut.toString().split(System.lineSeparator());
if(expect.length!=actual.length){
US>throw new RuntimeException("The number of lines output is different.\n"
+ "Expected number of rows\t["+expect.length+"]\n"
+ "Results\t["+actual.length+"]");
}
for (inti=0, length=actual.length;i<length;i++) {
if(!expect[i].equals(actual[i])){
row newRuntimeException("There is a difference in the results.\n"
+ "Expected\t["+expect[i]+"]\n"
+ US>"Results\t["+actual[i]+"]");
}
}
System.out.println("Congratulations");
}
}
When testing in the test
class in the question, the results are as follows:
Exception in thread "main" java.lang.RuntimeException—The results are different.
Expected value [1 1 3 4 5 14]
Results [1 1 3 4 5 14]
at TestTest.main (TestTest.java:48)
You can mechanically detect that there is a blank at the end, which is different from the expected result.
© 2024 OneMinuteCode. All rights reserved.