給定整數,反轉相加,重複直到得回文數。記錄每步結果。可用字符串模擬大數加法。
reverse(s.begin(),s.end())atoi(s.c_str()) 轉整數(若位數較少)或手工逐位相加s == reversed_s#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
bool isPalindrome(string s) {
string t=s; reverse(t.begin(),t.end()); return s==t;
}
string addStrings(string a, string b) {
reverse(a.begin(),a.end()); reverse(b.begin(),b.end());
string res; int carry=0;
for(int i=0;i>s) {
if (isPalindrome(s)) { cout<<0<
關鍵函數
stringstream 實現 int↔string 轉換
reverse 算法庫函數翻轉字符串
atoi(str.c_str()) C 風格轉整數
大數加法字符串模擬是無 BigInteger 環境下的通用技能。