CodeChef KOL1506上的运行时错误

时间:2016-08-01 06:22:10

标签: java runtime

当我在Eclipse上编译并运行此代码时,运行正常但在Codechef上它显示运行时错误。任何人都可以帮我找到为什么它不在网站上运行。网站链接为:https://www.codechef.com/viewsolution/10987533 问题链接是:https://www.codechef.com/problems/KOL1506/

import java.util.Scanner;

class SamosaBhai {
    public static void main(String[] args){
        int n =0;
        Scanner keyboard = new Scanner(System.in);
        Scanner input = new Scanner(System.in);
        System.out.println("Enter number of test Cases:");
        n = keyboard.nextInt();
        int[] ans = new int[n];
        for(int i = 0; i< n ;i++){

            //no. of houses and power 'd' is stored here
            String get = input.nextLine();
            String[] numarray = get.split(" "); // splitting string by spaces
            int num = Integer.parseInt(numarray[0]);  // number of houses
            int d =  Integer.parseInt(numarray[1]);     // power to be raised


            // positions of the houses is stored here
            String entry = input.nextLine();
            Scanner scanner = new Scanner(entry);
            int[] pos = new int[num];
            for (int j= 0;j<num;j++) {
                pos[j] = scanner.nextInt();
            }
            scanner.close();

            ans[i] = postalCharge(pos, d, n);

        }
        keyboard.close();
        input.close();
        for(int p =0; p<n; p++)
        System.out.println(ans[p]);

    }

    // function to calculate the postal charges between all the houses
    public static int postalCharge(int[] location, int d, int n){
        int total =0;
        for (int i = 0; i<n; i++){
            for(int j =0; j< n; j++){
                int n1 =  location[i];
                int n2  = location[j];
                total += Math.pow(Math.abs(n1-n2),d);
            }
        }
        return total;
    }
 }

0 个答案:

没有答案