• 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);
        }
    }
  • 相关阅读:
    SplitViewController的简单使用
    ViewController容器
    AnchorPoint 和Position 关系
    __OSX_AVAILABLE_BUT_DEPRECATED
    __OSX_AVAILABLE_STARTING
    UIButton重复点击,重复触发,怎么办
    iOS小技巧:用runtime 解决UIButton 重复点击问题
    FOUNDATION_EXPORT 或#define 或 extern
    nginx第一天
    053-001
  • 原文地址:https://www.cnblogs.com/EpisodeXI/p/4066087.html
Copyright © 2020-2023  润新知