天天看點

1024. Palindromic Number (25)

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;

bool judge(string s){
    string st = s;
    reverse(st.begin(), st.end());
    if(st == s) return true;
    else return false;
}

void add(string &s){
    string st = s;
    reverse(st.begin(), st.end());
    int c = 0;
    for (int i = (int)st.size() - 1; i >= 0; i--) {
        s[i] = s[i] + st[i] + c - '0';
        if(s[i] > '9'){
            s[i] = s[i] - 10;
            c = 1;
        }else c = 0;
        if((i == 0) && c == 1){
            s = "1" + s;
        }
    }
}

int main(){
    int k;
    string s, st;
    cin >> s >> k;
    if(judge(s)){
        cout << s << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= k; i++) {
        add(s);
        if (judge(s)) {
            cout << s << endl << i << endl;
            return 0;
        }
    }
    cout << s << endl << k << endl;

    return 0;
}

           

繼續閱讀