合肥工业大学oj 1331 回文数

//使用字符串加法
//类似恶魔A+B
#include<iostream>
#include<algorithm>
using namespace std;

string addition(string, string);

int main(){
    string n;
    while (cin >> n){
        while(true){
          string rev = n;
          reverse(rev.begin(), rev.end());
          cout << n;
          if(n == rev) break;
          n = addition(n, rev);
          cout << "--->";
        }
        cout << endl;
    }

    return 0;
}

string addition(string a, string b){
  string ans;
  int carry = 0;
  for(int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
      int x = i < 0 ? 0 : a[i] - '0';
      int y = j < 0 ? 0 : b[j] - '0';
      ans = (char)((x + y + carry) % 10 + '0') + ans;
      carry = (x + y + carry) / 10;
  }
  return ans;
}

//presented by 大吉大利,今晚AC

猜你喜欢

转载自blog.csdn.net/lalala_HFUT/article/details/88063411