在UVA中反转并添加运行时错误

时间:2014-01-30 10:16:17

标签: java

我的代码在UVA在线判断中遇到运行时错误,但它在我的编译器中运行正常。我不知道问题的确切位置。所以帮助我。以下是我的代码。

class reverseAdd {


public static int checkPalingdromMarker(long [] a1,long [] a2){
    int c=0;
    for(int q=0;q<a2.length/2;q++){

        if(a1[q]!=a2[q]){

            break;
        }
        else c++;

   }
    if(c==(a2.length/2))
    {
        return 1;
    }
    return 0;
}


public static void checkPalingdrom(long [] a1,long [] a2){
    int c=0;
    for(int q=0;q<a2.length/2;q++){

        if(a1[q]!=a2[q]){
            System.out.println("Not Palingdrom");
            break;
        }
        else c++;

   }
    if(c==(a2.length/2))
    {
        System.out.println("Palingdrom found");
    }

}

public static long [] reverseNumberDigits(long numToDigits){

        String string = Long.toString(numToDigits);
        long[] digits = new long[string.length()];
        /*
        for(int y = 0; y<string.length(); ++y){
          digits[y] = Long.parseLong(string.substring(y, y+1));
        }
        */
        int reverse=0,i=string.length();
        while( numToDigits != 0 )
          {
              reverse = reverse * 10;
              digits[i-1] = reverse + numToDigits%10;
              numToDigits = numToDigits/10;
              i=i-1;
          }



    return digits;
}

public static long  reverseNumber(long numberToReverse){
   long reverse = 0;

   while( numberToReverse != 0 )
      {
          reverse = reverse * 10;
          reverse = reverse + numberToReverse%10;
          numberToReverse = numberToReverse/10;

      }
   return reverse;

}


public static long [] readNumsFromCommandLine() {
    Scanner s = new Scanner(System.in);

    int count = s.nextInt();
    s.nextLine(); // throw away the newline.
    long [] numbers = new long[count];

    for(int i = 0; i < count; i++){
        if(s.hasNextLong()){
            numbers[i] = s.nextLong();
            s.nextLine();
        }
    }

    return numbers;
}

public static void main(String[] args) {
    long[] numbers = readNumsFromCommandLine();
    int inputLength=numbers.length;


    for(int f=0;f<inputLength;f++){    

        int countAddition=1;
        long a=numbers[f]+reverseNumber(numbers[f]);
        for(int x=0;x<1000;x++){

         System.out.println(a); 
         if(checkPalingdromMarker(reverseNumberDigits(reverseNumber(a)), reverseNumberDigits(a))==0){
             countAddition++;
             a=a+reverseNumber(a);
         }
         else{

             System.out.println(countAddition+" "+reverseNumber(a));
             break;
         }
        }
    }     

}       
}

1 个答案:

答案 0 :(得分:0)

UVa Online Judge对提交的代码有特定要求,以便正确编译。您的代码中有2个错误阻止了编译。

更改代码的第一行:

class reverseAdd { 

import java.util.*;
class Main {

注意:我运行了您的代码,这将导致UVa中的“错误答案”结果。

样本输入:

3
24 1
4358 754
305 794

预期输出为:

34
1998
1

您的代码(不正确)输出为:

4358 754
305 794
66
1 66
12892
42713
74437
147884
636625
1163261
2786872
7 2786872
808
1 808