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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章