1136 A Delayed Palindrome

 由于给出的数可能超出 long long范围,所以不能对两数直接求和,必须模拟加法运算过程,不然最后一个测试点无法通过。

中文版。

1079 延迟的回文数

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 bool isPalindrome(string str) {
 6     for(int i = 0; i < str.size()/2; ++i)
 7         if(str[i] != str[str.size()-1-i]) return false;
 8     return true;
 9 }
10 
11 int main() {
12     string a,b;
13     cin>>a;
14     int flag = 0;
15     for(int i = 0; i < 10; ++i) {
16         if(isPalindrome(a)) {
17             flag = 1;
18             break;
19         }
20         b = a;
21         reverse(b.begin(),b.end());
22         b.insert(0,a.size()-b.size(),'0');
23         int carry = 0;
24         string sum;
25         for(int i  = a.size()-1; i>=0; --i) {
26             sum += (a[i]-'0'+b[i]-'0'+carry)%10 +'0';
27             carry = (a[i]-'0'+b[i]-'0'+carry)/10;
28         }
29         if(carry != 0) sum += carry+'0';
30         reverse(sum.begin(),sum.end());
31         printf("%s + %s = ",a.c_str(),b.c_str());
32         a = sum;
33         printf("%s\n",a.c_str());
34     }
35     if(flag == 1) printf("%s is a palindromic number.\n",a.c_str());
36     else printf("Not found in 10 iterations.\n");
37     return 0;
38 }

1136 A Delayed Palindrome

 

上一篇:Err 1136 - Column count doesn't match value count at row 1


下一篇:SpringBoot之全局统一日期转换处理