PAT-A 1024. Palindromic Number
阿新 • • 發佈:2019-01-04
題意:給一個數字,不斷加上這個數字所有位反過來的數字,最多k次,問第幾次能變成迴文串。
會爆long long 。用字串來做就好。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; bool check(string n) { for(int i = 0; i < n.size()/2; i++) { if(n[i] != n[n.size() - i - 1]) return 0; } return 1; } int main() { string n; int k; cin >> n >> k; int i; string tmp; for(i = 0; i < k; i++) { if(check(n)) break; tmp = n; reverse(tmp.begin(), tmp.end()); int carry = 0; string res; for(int j = n.size()-1; j >= 0; j--) { int tt = n[j] - '0' + tmp[j] - '0' + carry; carry = tt / 10; res.insert(res.begin(), tt%10 + '0'); } if(carry) res.insert(res.begin(), carry + '0'); n = res; } cout << n << endl; cout << i << endl; return 0; }