PALIN-下一个回文-SPOJ问题

时间:2019-02-18 05:30:39

标签: java

我为Ridit开了一个帐户,Ridit是7岁的SPOJ学习Java的学生之一。我给他的第一个任务是PALIN-下一回文。这是解决此问题的链接-PALIN- The next Palindrome- SPOJ在我向他解释之后,除了能够消除前导零以外,他几乎可以解决所有问题。以下是他对问题的解决方案-

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Scanner in = new Scanner(System.in);
            int t = Integer.parseInt(in.nextLine());
            String[] numbersInString = new String[t];

            for (int i = 0; i <t; i++) {
                String str = in.nextLine();
                numbersInString[i] = removeLeadingZeros(str);
            }

            for (int i = 0 ; i<t; i++) {
                int K = Integer.parseInt(numbersInString[i]);
                int answer = findTheNextPalindrome(K);
                System.out.println(answer);

            }
        }catch(Exception e) {
            return;
        }
    }
    static boolean isPalindrome(int x) {
        String str = Integer.toString(x);
        int length = str.length();
        StringBuffer strBuff = new StringBuffer();
        for(int i = length - 1;i>=0;i--) {
            char ch = str.charAt(i);
            strBuff.append(ch);
        }
        String str1 = strBuff.toString();
        if(str.equals(str1)) {
            return true;
        }

        return false;

    }

    static int findTheNextPalindrome(int K) {
        for(int i = K+1;i<9999999; i++) {
            if(isPalindrome(i) == true) {
                return i;
            }
        }
        return -1;
    }


    static String removeLeadingZeros(String str) {
        String retString = str;
        if(str.charAt(0) != '0') {
            return retString;
        }
        return removeLeadingZeros(str.substring(1));
    }

}

它在他的计算机上的Eclipse中给出了正确的答案,但在SPOJ中却失败了。如果有人在第一次投稿时帮助这个小男孩,那肯定会让他非常高兴。我找不到此解决方案的任何问题...预先感谢...

1 个答案:

答案 0 :(得分:0)

这可能有帮助

import java.io.IOException;
import java.util.Scanner;

public class ThenNextPallindrom2 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        int t = 0;
        Scanner sc = new Scanner(System.in);

        if(sc.hasNextInt()) {
            t = sc.nextInt();
        }
        sc.nextLine();

        int[] arr, arr2;
        while(t > 0) {
            t--;
            String s = sc.nextLine();
            arr = getStringToNumArray(s);

            if(all9(arr)) {
                arr2 = new int[arr.length + 1];
                arr2[0] = 1;
                for(int i=0;i<arr.length;i++) {
                    arr2[i+1] = 0;
                }   
                arr2[arr2.length -1] = 1;
                arr = arr2;
            } else{
                int mid = arr.length/ 2;
                int left = mid-1;
                int right = arr.length % 2 == 1 ? mid + 1 : mid;
                boolean left_small = false;

                while(left >= 0 && arr[left] == arr[right]) {
                    left--;
                    right++;
                }

                if(left < 0 || arr[left] < arr[right]) left_small = true;

                if(!left_small) {

                    while(left >= 0) {
                        arr[right++] = arr[left--];
                    }

                } else {
                    mid = arr.length/ 2;
                    left = mid-1;
                    int carry = 1;

                    if(arr.length % 2 == 0) {
                        right = mid;
                    } else {
                        arr[mid] += carry;
                        carry = arr[mid]/10;
                        arr[mid] %= 10;
                        right = mid + 1;
                    }

                    while(left >= 0) {
                        arr[left] += carry;
                        carry = arr[left] / 10;
                        arr[left] %= 10;
                        arr[right++] = arr[left--];
                    }

                }

            }           
            printArray(arr);

        }

    }


    public static boolean all9(int[] arr) {
        for(int i=0;i<arr.length;i++) {
            if(arr[i] != 9)return false;
        }
        return true;
    }

    public static void printArray(int[] arr) {
        for(int i=0;i<arr.length;i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
    }

    public static int[] getStringToNumArray(String s) {
        int[] arr = new int[s.length()];

        for(int i=0; i<s.length();i++) {                
            arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));  
        }

        return arr;

    }

}