我试图解决HackerEarth的挑战。我试图提交此代码但它在网站上显示NZEC运行时错误。我在不同的PC上运行此代码,它工作正常,并为所有测试用例提供正确的输出。
挑战是在给定的输入整数中找到最小整数,以便当该整数替换所有其他整数时,所有这些整数的总和应超过输入数字的总和。
输入以两行的形式给出,首先包含整数的总数,然后是用空格分隔的整数
例如:
INPUT
5
1 2 3 4 5
输出
4
说明
1 + 2 + 3 + 4 + 5 = 15
4 + 4 + 4 + 4 + 4 = 16
所以根据问题,这些整数中最小的是4.所以代码应该给出输出4。
我知道NZEC是非零错误代码,但我想知道为什么它会出现在我的代码中以及为什么其他系统没有问题。
import java.io.*;
import java.util.*;
class Abc
{
public static void main(String args[] ) throws Exception
{
Console con = System.console ();
int n = Integer.parseInt(con.readLine());
Console conn = System.console ();
String str = conn.readLine();
int arr[] = new int [n];
StringTokenizer st = new StringTokenizer (str);
for (int x=0; x<n; x++)
{
int N = Integer.parseInt(st.nextToken());
arr[x] = N ;
}
int iniSum = 0;
for (int x=0; x<n; x++)
{
iniSum += arr[x];
}
int tempArr [] = new int [n];
int tempArrPos = 0;
for (int x=0; x<n; x++)
{
int num = arr [x];
int checkSum = num*n;
//System.out.print (checkSum+"\n");
if (checkSum>iniSum)
{
tempArr [tempArrPos] = num;
//System.out.print ("\n\n\t"+tempArr [tempArrPos]);
tempArrPos++;
}
}
//for (int xx = 0; xx < n; xx++)
// System.out.print(tempArr[xx]);
int smallest = 1000000000;
for(int i=0; i<n; i++)
{
if(tempArr[i] < smallest && tempArr[i]>0)
smallest = tempArr[i];
}
System.out.print(smallest);
}
}