【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.");
    }
}

总结,作者水平有限,有哪里有问题都欢迎评论指出。如果对你有帮助欢迎点赞。

发布了39 篇原创文章 · 获赞 79 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_40515692/article/details/103264330