英文:
Runtime Error in Google CodeJam 2020 Vestigium
问题
Vestigium 在拉丁文中的意思是“痕迹”。
方阵的痕迹是主对角线上值的总和(主对角线从左上到右下)。
一个 N × N 的方阵是拉丁方阵,如果每个单元格包含 N 种不同的值之一,并且在行或列内不重复。在这个问题中,我们只处理 N 个值为 1 到 N 之间的“自然拉丁方阵”。
给定一个仅包含 1 到 N 之间整数的矩阵,我们想计算它的痕迹,并检查它是否为一个自然拉丁方阵。为了提供一些额外的信息,与其仅告诉我们矩阵是否为自然拉丁方阵,不如计算包含重复值的行数和列数。
import java.io.*;
import java.util.*;
class Solution
{
public static void main(String args[])
{
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T, N, matrix[][], dupCheck[];
int i, j, k, r, c, Sum;
String inMat[];
dupCheck = new int[100];
T = Integer.parseInt(sc.nextLine());
for (k = 1; k <= T; k++, matrix = null)
{
N = Integer.parseInt(sc.nextLine());
matrix = new int[N][N];
for (i = 0, r = 0; i < N; i++)
{
inMat = (sc.nextLine()).split(" ");
Arrays.fill(dupCheck, 0);
for (j = 0; j < N; j++)
{
matrix[i][j] = Integer.parseInt(inMat[j]);
dupCheck[matrix[i][j]]++;
}
for (j = 0; j < 100; j++)
{
if (dupCheck[j] > 1)
{
r++;
break;
}
}
}
for (i = 0, Sum = 0; i < N; i++)
Sum += matrix[i][i];
for (i = 0, c = 0; i < N; i++)
{
Arrays.fill(dupCheck, 0);
for (j = 0; j < N; j++)
{
dupCheck[matrix[j][i]]++;
}
for (j = 0; j < 100; j++)
{
if (dupCheck[j] > 1)
{
c++;
break;
}
}
}
System.out.println("Case #" + k + ": " + Sum + " " + r + " " + c);
}
}
}
在 IDE 中运行正常且得到正确答案。但是当我提交时,出现运行时错误。
英文:
Vestigium means "trace" in Latin.
The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).
An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.
Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.
import java.io.*;
import java.util.*;
class Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T,N,matrix[][],dupCheck[];
int i,j,k,r,c,Sum;
String inMat[];
dupCheck=new int[100];
T=Integer.parseInt(sc.nextLine());
for(k=1;k<=T;k++,matrix=null)
{
N=Integer.parseInt(sc.nextLine());
matrix=new int[N][N];
for(i=0,r=0;i<N;i++)
{
inMat=(sc.nextLine()).split(" ");
Arrays.fill(dupCheck,0);
for(j=0;j<N;j++)
{
matrix[i][j]=Integer.parseInt(inMat[j]);
dupCheck[matrix[i][j]]++;
}
for(j=0;j<100;j++)
{
if(dupCheck[j]>1)
{
r++;
break;
}
}
}
for(i=0,Sum=0;i<N;i++) Sum+=matrix[i][i];
for(i=0,c=0;i<N;i++)
{
Arrays.fill(dupCheck,0);
for(j=0;j<N;j++)
{
dupCheck[matrix[j][i]]++;
}
for(j=0;j<100;j++)
{
if(dupCheck[j]>1)
{
c++;
break;
}
}
}
System.out.println("Case #"+k+": "+Sum+" "+r+" "+c);
}
}
}
It runs fine in IDE and also gives Correct Answer.
But I am getting a Runtime Error when I submit.
答案1
得分: 0
尝试在关闭主方法之前添加 sc.close();。
英文:
Try Adding sc.close(); before closing main method.
答案2
得分: 0
运行时错误是因为您的代码在处理第一个案例时运行正常,然后给出答案。然后,当我们输入另一个案例时,它也会给出答案。但是根据 Google 的要求,他们会一次性输入所有案例,然后您需要逐行为所有案例给出答案。
英文:
The Runtime error is coming because your code is working for the first case, then giving answer. Then when we enter another case, it also gives an answer. But by Google they will enter all cases at once and then you need to give the answers for all the cases in single-single lines.
专注分享java语言的经验与见解,让所有开发者获益!
评论