1079 延迟的回文数 (20 分)

给定一个 k+1 位的正整数 N,写成 ak​​a1​​a0​​ 的形式,其中对所有 i 有 0ai​​<10 且 ak​​>0。N 被称为一个回文数,当且仅当对所有 i 有 ai​​=aki​​。零也被定义为一个回文数。

非回文数也可以通过一系列操作变出回文数。首先将该数字逆转,再将逆转数与该数相加,如果和还不是一个回文数,就重复这个逆转再相加的操作,直到一个回文数出现。如果一个非回文数可以变出回文数,就称这个数为延迟的回文数。(定义翻译自 https://en.wikipedia.org/wiki/Palindromic_number )

给定任意一个正整数,本题要求你找到其变出的那个回文数。

输入格式:

输入在一行中给出一个不超过1000位的正整数。

输出格式:

对给定的整数,一行一行输出其变出回文数的过程。每行格式如下

A + B = C

其中 A 是原始的数字,B 是 A 的逆转数,C 是它们的和。A 从输入的整数开始。重复操作直到 C 在 10 步以内变成回文数,这时在一行中输出 C is a palindromic number.;或者如果 10 步都没能得到回文数,最后就在一行中输出 Not found in 10 iterations.

输入样例 1:

97152

输出样例 1:

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

输入样例 2:

196

输出样例 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.
 
//c
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1100;
char a[maxn] = {0},b[maxn] = {0};

bool isPalindromic(char a[]){
    int len = strlen(a);
    for(int i = 0; i < len/2; i++){
        if(a[i] != a[len - i- 1]) return false;
     }
     return true;
}

void reverse(char a[],char b[]){  //reverse a to b
    int len = strlen(a);
    for(int i = 0; i < len; i++){
        b[len - i - 1] = a[i];
    }
    //b[len] = '';
}

void addToA(char a[],char b[]){
    int len = strlen(a);
    int carry = 0;
    for(int i = 0; i < len; i++){
        int temp = (a[i] - '0') + (b[i] - '0')+ carry;
        b[i] = temp % 10 + '0';
        carry = temp / 10; 
    }
    if(carry != 0) b[len] = carry + '0';
    reverse(b,a);
    //carry[len] = '';
}

int main(){    
    //fgets(a,maxn,stdin);
    cin.getline(a,maxn);
    int cnt = 0;
    while(isPalindromic(a) == false && cnt < 10){
        reverse(a,b);
        printf("%s + %s = ",a,b);
        addToA(a,b);
        printf("%s
",a);
        cnt++;
    }
    if(isPalindromic(a)){
        printf("%s is a palindromic number.",a);
    }else{
        printf("Not found in 10 iterations.");
    }
    return 0;
}
//C++
#include<iostream>
#include<algorithm>
using namespace std;

bool isPalindromic(const string &C){
    int len = C.size();
    for(int i = 0; i < len/2; i++){
        if(C[i] != C[len - i - 1]) return false;
    }
    return true;
}

string add(const string &A,const string &B){
    string C;
    int len = A.size();
    int carry = 0;
    for(int i = len - 1; i >= 0; i--){
        int temp = A[i] - '0' + B[i] - '0' + carry;
        C += temp % 10 + '0';
        carry = temp / 10;
    }
    if(carry) C += '0' + carry;
    reverse(C.begin(),C.end());
    return C;
}

int main(){
    string A,B,C;
    cin >> A;
    int cnt = 10;
    if(isPalindromic(A)){
        cout << A << " is a palindromic number.";
        return 0;
    }
    while(cnt--){
        B = A;
        reverse(B.begin(),B.end());
        C = add(A,B);
        cout << A << " + " << B << " = " << C << endl;
        if(isPalindromic(C)) {
            cout << C << " is a palindromic number.";
            return 0;
        }
         A = C;   
    }
    cout << "Not found in 10 iterations." << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/wanghao-boke/p/10424605.html