PAT 1024

这道题主要就是需要写一个大整数加法,没什么难度

package pat.pat_1024;

import java.util.*;
import java.io.*;

class FastReader{
    BufferedReader reader;
    StringTokenizer tokenizer;
    
    public FastReader(InputStream stream){
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }
    
    public String next(){
        while (tokenizer == null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (Exception e){
                throw new RuntimeException(e);
            }
        }
        
        return tokenizer.nextToken();
    }
    
    public int next_int(){
        return Integer.parseInt(next());
    }
}

public class Main {
    static String str_add(String op1, String op2){
        int carry = 0;
        int size1 = op1.length();
        int size2 = op2.length();
        
        StringBuilder sb = new StringBuilder();
        int cur1 = size1 - 1, cur2 = size2 - 1;
        while (cur1 >= 0 || cur2 >= 0){
            int cur_num1 = 0, cur_num2 = 0;
            if (cur1 >= 0)
                cur_num1 = op1.charAt(cur1) - '0';
            if (cur2 >= 0)
                cur_num2 = op2.charAt(cur2) - '0';
            
            int cur_num = cur_num1 + cur_num2 + carry;
            
            carry = cur_num / 10;
            cur_num %= 10;
            
            sb.append((char)(cur_num + '0'));
            
            cur1--;
            cur2--;
        }
        
        if (carry > 0)
            sb.append((char)(carry + '0'));
        return sb.toString();
    }
    
    static String reverse_str(String str){
        int size = str.length();
        Stack<Character> ans_stack = new Stack<Character>();
        
        for (int i = 0; i < size; i++)
            ans_stack.push(str.charAt(i));
        
        StringBuilder sb = new StringBuilder();
        while (!ans_stack.isEmpty())
            sb.append(ans_stack.pop());
        
        return sb.toString();
    }
    
    static boolean is_palindromic(String str){
        int size = str.length();
        for (int i = 0; i < size / 2; i++){
            if (str.charAt(i) != str.charAt(size - 1 - i))
                return false;
        }
        
        return true;
    }
    
    public static void main(String[] args){
        FastReader reader = new FastReader(System.in);
        String N;
        int K;
        
        N = reader.next();
        K = reader.next_int();
        
        if (is_palindromic(N)){
            System.out.println(N);
            System.out.println(0);
            
            return;
        }
        
        String order_str = N;
        for (int i = 1; i <= K; i++){
            String rev_str = reverse_str(order_str);
            String sum_str = reverse_str(str_add(order_str, rev_str));
            
            if (is_palindromic(sum_str)){
                System.out.println(sum_str);
                System.out.println(i);
                return;
            }
            
            order_str = sum_str;
        }
        
        System.out.println(order_str);
        System.out.println(K);
    }
}
原文地址:https://www.cnblogs.com/EpisodeXI/p/4066087.html