From 7963931db411a1ce21ce09e5fa3de2b9db37a0ef Mon Sep 17 00:00:00 2001 From: hao14293 Date: Wed, 5 Dec 2018 15:55:11 +0800 Subject: [PATCH] =?UTF-8?q?Create=201079.=E5=BB=B6=E8=BF=9F=E7=9A=84?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E6=95=B0.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PAT/PAT-B/CPP/1079.延迟的回文数.cpp | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 PAT/PAT-B/CPP/1079.延迟的回文数.cpp diff --git a/PAT/PAT-B/CPP/1079.延迟的回文数.cpp b/PAT/PAT-B/CPP/1079.延迟的回文数.cpp new file mode 100644 index 0000000..3c0f809 --- /dev/null +++ b/PAT/PAT-B/CPP/1079.延迟的回文数.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +bool isPal(string s){ + for(int i = 0; i < s.length() / 2; i++){ + if(s[i] != s[s.length() - i - 1]) + return false; + } + return true; +} +string add(string a, string b){ + string result; + int num = 0, carry = 0; + for(int i = 0; i < a.length(); i++){ + num = (a[i] - '0' + b[i] - '0') + carry; + if(num >= 10){ + num -= 10; + carry = 1; + }else { + carry = 0; + } + result += (num + '0'); + } + if(carry == 1) result += '1'; + reverse(result.begin(), result.end()); + return result; +} +int main(){ + string a, b, c; + cin >> a; + int cnt = 1; + while(!isPal(a) && cnt <= 10){ + cnt++; + b = a; + reverse(b.begin(), b.end()); + c = add(a, b); + cout << a << " + " << b << " = " << c << endl; + a = c; + } + if(cnt <= 10) cout << a << " is a palindromic number." << endl; + else cout << "Not found in 10 iterations." << endl; + return 0; +}