2015-03-30 99 views
-1

這個特定的代碼是針對spoj中下一個迴文的問題的解決方案。 這是鏈接http://www.spoj.com/problems/PALIN/ 該問題提到,數字不會跨越1000000.So long應該足夠了,但仍然會出現此錯誤。獲取NZEC運行時錯誤。無法識別錯誤

import java.util.*; 

    import java.lang.*; 

    import java.io.*; 

    class Ideone 

    { 
     public static long tostr(String s,String b){ 

      StringBuilder a = new StringBuilder(s); 

      String r = String.valueOf(a.reverse()); 

      return Long.parseLong(s+b+r); 

     } 

     public static long toStr(String s){ 

      StringBuilder a = new StringBuilder(s); 

      String r = String.valueOf(a.reverse()); 

      return Long.parseLong(s+r); 

     } 

     public static void main (String[] args) throws java.lang.Exception 

     { 

      BufferedReader br = new BufferedReader(new 

    InputStreamReader(System.in)); 

      PrintWriter out = new PrintWriter(System.out,true); 

      int t = Integer.parseInt(br.readLine()); 

      while(t-->0){ 

       long n = Long.parseLong(br.readLine()); 

       long number = n; 


       int len = 0; while (number != 0) { number /= 10; len++; } 

       if((len&1)==0){ 

        String s = String.valueOf(n); 

        String r = s.substring(0,len/2); 

        while(true){ 

         long num = toStr(r); 


         if(num == 99){ 
          out.println(101);break; 

         } 
         if(num == 9999){ 
          out.println(10001);break; 
         } 
         if(num == 999999){ 
          out.println(1000001);break; 
         } 
         if(num>n) 
          {out.println(num);break;} 
         long m = Long.parseLong(r); 
         m++;  
         r = String.valueOf(m); 


       } 
      } 

      else{ 

       String s = String.valueOf(n); 
       String r = s.substring(0,len/2); 

       String b = s.substring((int)(len/2),(len/2)+1); 

       if(b.equals("9")){ 

        while(true){ 

         if(n == 999){out.println(1001);break;} 

         if(n==99999){out.println(100001);break;} 

         if(n == 9999999){out.println(10000001);break;} 

         long num = tostr(r,b); 

         if(num>n){out.println(num);break;} 

         long m = Long.parseLong(r); 

         m++; 

         r = String.valueOf(m); 
        } 
       } 
       else{ 

     while(true){ 

         long num = tostr(r,b); 

         if(num>n){out.println(num);break;} 

         long m = Long.parseLong(b); 

         m++; 

         b = String.valueOf(m); 

        } 

       } 

      } 

     } 

    } 

    } 

回答

0

Javalong不足。迴文可能高達1000000 數字長。這遠遠超出了Java long的大小。仔細重新閱讀這個問題。