天天看点

PAT Advanced 1024 Palindromic Number

1024 Palindromic Number

A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

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. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

Input Specification:

Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤1010) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

Output Specification:

For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

Sample Input 1:

67 3
           

Sample Output 1:

484
2  
           

Sample Input 2:

69 3
           

Sample Output 2:

1353
3
           

解题思路

这道题本身是没有难度的,但是不注意容易拿不全分。 给定一个数,判断是否是 回文的,如果不是反转与原数相加。但是相加的次数收到 k的限制。 本题 存在反转后大数相加的问题, long long型也是不可以的(我试过) 需要模拟两个数相加的过程。

解题代码

写法一:(存在后几个实在没找到 😢 ) ,发现了的可以帮忙看看

#include <iostream>
#include <algorithm>
using namespace std;
int n, k, i;
string add(string s, string st){
    string ans = "";
    int t = 0;
    for (int i = 0; i < s.size() || i < st.size(); i ++){
        if (i < s.size()) t += s[i] - '0' ;
        if (i < st.size()) t += st[i] - '0';
        ans += t % 10 + '0';
        t /= 10;
    }
    if (t) ans += t + '0';
    reverse(ans.begin(), ans.end());
    return ans;
}
int main(){
    scanf("%d %d", &n, &k);
    string s = to_string(n);
    for ( i = 0; i < k; i++){
        string st = s;
        reverse(st.begin(), st.end());
        if (st == s) break;
        s = add(s, st);
    }
    printf("%s\n%d", s.c_str(), i);
    return 0;
}
           

写法二(AC):

#include <algorithm>
#include <iostream>
using namespace std;
int  n, i;
string add(string s, string st){
    int t = 0;
    for (int i = s.size() - 1; i >= 0; i--){
        int x = s[i] - '0' + st[i] - '0';
        s[i] = (x + t) % 10 + '0';
        t = (x + t) / 10;
    }
    if (t) s = '1' + s;
    return s;
}
int main(){
    string s, st;
    cin >> s >> n;
    for (i = 0; i < n; i++){
        st = s;
        reverse(st.begin(), st.end());
        if (st == s) break;
        s = add(s, st);
    }
    printf("%s\n%d", s.c_str(), i);
}
           

参考写法(字符串处理的地方值得参考):

#include <algorithm>
#include <iostream>
using namespace std;
string  st;
int cnt, n, i;
void add (string s){
    int t = 0;
    for (int i = 0; i < s.size(); i++){
        st[i] = st[i] + s[i] + t - '0';
        t = 0;
        if (st[i] > '9'){
            st[i] -= 10;
            t = 1;
        }
    }
    if (t) st += '1';
    reverse(st.begin(), st.end());
}
int main(){
    string s;
    cin >> st >> n;
    for ( i = 0; i < n; i++){
        s = st;
        reverse(s.begin(), s.end());
        if (st == s) break;
        add(s);
    }
    printf("%s\n%d", st.c_str(), i);
}
           

继续阅读