为什么我在Google Code Jam中遇到运行时错误?

huangapple go评论96阅读模式
英文:

Why am I getting a run time error in Google code jam?

问题

class Main 
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int T=sc.nextInt();
        for(int z=1;z<=T;z++)
        {

            int size=sc.nextInt();
            int mat[][]=new int[size][size];

            for(int i=0;i<size;i++)
            {
                for(int j=0;j<size;j++)
                {
                    mat[i][j]=sc.nextInt();
                }
            }

            int k=trace(mat,size);

            int r=row_duplicate(mat,size);

            int c=col_duplicate(mat,size);

            System.out.println("Case #" +z+ ":"+" "+k+ " " +r+ " " +c); //Case #1: 4 0 0
        }
    }

    public static int trace(int arr[][],int size)
    {
        int sum=0;
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                if(i==j)
                    sum=sum+arr[i][j];
            }
        }
        return sum;
    }
    public static int row_duplicate(int arr[][],int size)
    {
        Hashtable<Integer,Integer> h=new Hashtable<>();
        int count=0;
        for(int i=0;i<size;i++)
        {
            int row[]=arr[i];
            for(int j=0;j<row.length;j++)
            {
                if(h.containsKey(row[j]))
                {
                    count++;
                    break;
                }
                else
                {
                    h.put(row[j],1);
                }
            }
            h.clear();
        }
        return count;
    }

    public static int col_duplicate(int arr[][],int size)
    {
        Hashtable<Integer,Integer> h=new Hashtable<>();
        int count=0;
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                int ele=arr[j][i];
                if(h.containsKey(ele))
                {
                    count++;
                    break;
                }
                else
                {
                    h.put(ele,1);
                }
            }
            h.clear();
        }
        return count;
    }
}
英文:

Vestigium means "trace" in Latin. In this problem, we work with Latin squares and matrix traces.
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.

It runs in Intellij

class Main 
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int T=sc.nextInt();
for(int z=1;z&lt;=T;z++)
{
int size=sc.nextInt();
int mat[][]=new int[size][size];
for(int i=0;i&lt;size;i++)
{
for(int j=0;j&lt;size;j++)
{
mat[i][j]=sc.nextInt();
}
}
int k=trace(mat,size);
int r=row_duplicate(mat,size);
int c=col_duplicate(mat,size);
System.out.println(&quot;Case #&quot; +z+ &quot;:&quot;+&quot; &quot; +k+ &quot; &quot; +r+ &quot; &quot; +c); //Case #1: 4 0 0
}
}
public static int trace(int arr[][],int size)
{
int sum=0;
for(int i=0;i&lt;size;i++)
{
for(int j=0;j&lt;size;j++)
{
if(i==j)
sum=sum+arr[i][j];
}
}
return sum;
}
public static int row_duplicate(int arr[][],int size)
{
Hashtable&lt;Integer,Integer&gt; h=new Hashtable&lt;&gt;();
int count=0;
for(int i=0;i&lt;size;i++)
{
int row[]=arr[i];
for(int j=0;j&lt;row.length;j++)
{
if(h.containsKey(row[j]))
{
count++;
break;
}
else
{
h.put(row[j],1);
}
}
h.clear();
}
return count;
}
public static int col_duplicate(int arr[][],int size)
{
Hashtable&lt;Integer,Integer&gt; h=new Hashtable&lt;&gt;();
int count=0;
for(int i=0;i&lt;size;i++)
{
for(int j=0;j&lt;size;j++)
{
int ele=arr[j][i];
if(h.containsKey(ele))
{
count++;
break;
}
else
{
h.put(ele,1);
}
}
h.clear();
}
return count;
}
}

答案1

得分: 2

我认为你应该提到你的类名为'Solution'。
有关更多信息,请查看链接

英文:

I think you should mention your class name 'Solution'.
for further information go through the link

答案2

得分: 0

问题在于你没有正确地导入Scanner类。
在顶部添加这行代码:import java.util.*;

英文:

The problem is that you are not importing Scanner class properly.
Add this line at the top: import java.util.*;

答案3

得分: 0

将类名从Main更改为Solution

import java.util.*;
import java.io.*;

class Solution {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		
		int t = sc.nextInt();
		
		int k = 1;
		while (t > 0) {
			int n = sc.nextInt();
		
			int m[][] = new int[n][n];
			int rows = 0, columns = 0;	
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					m[i][j] = sc.nextInt();
				}
			}
			
			int sum = 0;
			for (int i = 0; i < n; i++) {
				sum += m[i][i];
			}
			
			for (int i = 0; i < n; i++) {
				HashSet<Integer> h = new HashSet<>();
				for (int j = 0; j < n; j++) {
					h.add(m[i][j]);
				}
				if (h.size() < n) {
					rows++;
				}
			}
			
			for (int i = 0; i < n; i++) {
				HashSet<Integer> h = new HashSet<>();
				for (int j = 0; j < n; j++) {
					h.add(m[j][i]);
				}
				if (h.size() < n) {
					columns++;
				}
			}
			
			System.out.printf("Case #%d: %d %d %d\n", k, sum, rows, columns);
			k++;
			t--;
		}
	}
}
英文:

Change the class name to Solution instead of Main

i.e

import java.util.*;
import java.io.*;
class Solution{
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int k =1;
while(t &gt; 0)
{
int n = sc.nextInt();
int m[][] = new int[n][n];
int rows = 0, columns = 0;	
for(int i = 0 ; i &lt; n ; i++)
{
for(int j = 0 ; j &lt; n ;j++)
{
m[i][j] = sc.nextInt();
}
}
int sum = 0;
for(int i = 0 ; i &lt; n ; i++)
{
sum+= m[i][i];
}
for(int i = 0 ; i &lt; n ; i++)
{
HashSet&lt;Integer&gt; h = new HashSet&lt;&gt;();
for(int j = 0 ; j &lt; n ; j++)
{
h.add(m[i][j]);
}
if(h.size() &lt; n)
{
rows++;
}
}
for(int i = 0 ; i &lt; n ; i++)
{
HashSet&lt;Integer&gt; h = new HashSet&lt;&gt;();
for(int j = 0 ; j &lt; n ; j++)
{
h.add(m[j][i]);
}
if(h.size() &lt; n)
{
columns++;
}
}
System.out.printf(&quot;Case #%d: %d %d %d\n&quot;, k, sum, rows, columns);
k++;
t--;
}
}
}

答案4

得分: -1

这是代码的Python实现,对所有测试用例都有效。如果遇到问题,请随时提问。

T = int(input())
for i in range(1, T + 1):
    size = int(input())
    column = []
    r = 0
    c = 0
    k = 0
    for a in range(size):
        row = list(map(int, input().split()))
        column.append(row)
    for q in range(size):
        k = k + column[q][q]
    for q in column:
        for z in q:
            if q.count(z) > 1:
                r = r + 1
                break
    column = [[column[j][i] for j in range(len(column))] for i in range(len(column[0]))]

    for q in column:
        for z in q:
            if q.count(z) > 1:
                c = c + 1
                break
    i = str(i)
    print("Case #" + i + ":", k, r, c)
英文:

here is the python implementation of the code, this is working for all the test cases
feel free to ask if you encounter an issue

T = int(input())
for i in range(1,T+1):
size=int(input())
column =[]
r = 0
c = 0
k = 0
for a in range(size):
row = list(map(int,input().split()))
column.append(row)
for q in range(size):
k=k+column[q][q]
for q in column:
for z in q:
if q.count(z)&gt;1:
r = r+1
break
column = [[column[j][i] for j in range(len(column))] for i in range(len(column[0]))]
for q in column:
for z in q:
if q.count(z)&gt;1:
c = c+1
break
i=str(i)
print(&quot;Case #&quot;+i+&quot;:&quot;,k,r,c)

huangapple
  • 本文由 发表于 2020年4月4日 23:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61030432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定