【PAT算法之路】 -- 大整數 1136 A Delayed Palindrome (20 分) C++ JAVA解法

【PAT算法之路】 – 專欄總攬

大整數在PAT中考的較少,但是也可能會考,在考PAT前,我是準備遇到了就直接Java的,畢竟記C++的寫法心累 >﹏< 。話說都C++11/14/17/20了,給個高精度庫行不行。這次來舉一個常見的迴文相關的大整數題,用的C++,實現的是相等數位的正整數相加。最後在介紹下Java的大整數類。

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k+1 digits a
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

Given any positive integer, you are supposed to find its paired palindromic number.

Input Specification:

Each input file contains one test case which gives a positive integer no more than 1000 digits.

Output Specification:

For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:

A + B = C

where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number – in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.

Sample Input 1:

97152

Sample Output 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 2:

196

Sample Output 2:

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

題目意思很簡單,看示例就明白了。

容我先給出代碼

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isPalin(string str){
    string str2 = str;
    reverse(str.begin(),str.end());
    return str == str2;
}

string plusStr(string s1,string s2){
    int c = 0;
    string res;
    for(int i=s1.size()-1;i>=0;i--){
        int ans = c+s1[i]+s2[i]-'0';
        c = 0;
        if(ans > '9'){
            c = 1;
            ans -= 10;
        }
        res += ans;
    }
    if(c == 1)
        res += "1";
    reverse(res.begin(),res.end());
    return res;
}

int main() {
    string str;
    cin >> str;
    if(isPalin(str)){
        cout << str << " is a palindromic number.";
        return 0;
    }

    string res = str;
    int time = 10;
    while(!isPalin(res) && time--){
        string temp = res;
        reverse(temp.begin(),temp.end());
        cout << res << " + " << temp << " = ";
        res = plusStr(res,temp);
        cout << res << endl;
    }

    if(time == -1 && !isPalin(res))
        cout << "Not found in 10 iterations.";
    else
        cout << res << " is a palindromic number.";
    return 0;
}

一、函數plusStr的實現

數位相等的數相加,只需從低位到高位相加然後再倒置(記得要處理最後一個進位c,如果有進位要在末尾+’1’)就可以了。

JAVA版

貌似其他沒有什麼說的,再附上Java大整數解法吧。用vscode寫的,果然加上自動補全、錯誤提示的話,沒有code::block順暢,不過沒有這些又不會寫( ╯□╰ )

  • Stringfinal類不能被繼承且爲字符串常量,而StringBuilderStringBuffer均爲字符串變量。所以要改變字符串同時操作較複雜時一般用StringBuilder,而且它的方法也多是這方面的,就比如reverse
  • 背下輸入吧,Scanner input = new Scanner(System.in);,然後input.可以看到可以讀取很多不同類型的信息,還有很多中輸入,這種感覺挺方便的。
  • 大名鼎鼎的BigInteger,背下吧,記住Java沒有運算符重載,所以所有常用的操作都是以方法的形式。如.add()
  • System.out.print不帶回車,System.out.println帶回車,
  • 字符串比較用.equals(),Java裏面可以直接字符串相加,甚至可以字符串加上其他類型(相當於那個類型調用了toString()),
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        
        int time = 10;
        for (int i = 0; i < time; i++) {
            StringBuilder temp = new StringBuilder(str);
            String str2 = temp.reverse().toString();

            if(str.equals(str2)){
                System.out.print(str+" is a palindromic number.");
                return;
            }

            BigInteger b1 = new BigInteger(str);
            System.out.print(b1);
            b1 = b1.add(new BigInteger(str2));
            str = b1.toString();
            System.out.println(" + "+str2+" = "+str);
        }
        
        System.out.print("Not found in 10 iterations.");
    }
}

總結,作者水平有限,有哪裏有問題都歡迎評論指出。如果對你有幫助歡迎點贊。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章