在CodeChef上运行此代码时,出现NZEC类型错误

时间:2020-04-15 07:14:12

标签: java

我认为它与无限循环有关,但我似乎找不到错误。甚至我的测试用例数量也很有限,然后出现错误。它可能是数组超出范围的异常,但我找不到这种异常。我刚刚开始编码并意识到我的错误。

import java.util.Scanner;

Class SellCars{


public static void main(String[] args){

    int profit=0;int max=0;int count=0;
    Scanner in = new Scanner(System.in);
    int Testcase = in.nextInt();

    for(int i=0;i<Testcase;i++)
    {
            profit=0;



    int Cars = in.nextInt();
    int[] prices =  new int[Cars];

    for(int p=0;p<Cars;p++)
    {
        prices[p] = in.nextInt();
    }

        for(int j =0;j<Cars;j++) {
            max = 0;

            for (int l : prices) {
                if (l > max) {
                    max = l;

                }
            }

            profit += max;

            for (int a = 0; a < prices.length; a++)
            {

                if(prices[a]==max) {
                    if (count == 0) {
                        prices[a] = 0;
                        count++;
                    }
                }

            }

           for(int b=0;b<prices.length;b++)
           {
               if(prices[b]!=0)
               {
                   prices[b]-=1;
               }
           }

        }
        System.out.println(profit);
    }


}

}

0 个答案:

没有答案