【1136】A Delayed Palindrome (20分)

简介: 【1136】A Delayed Palindrome (20分)【1136】A Delayed Palindrome (20分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include<string>
/*
/*注意1:字符串存数字时低位存数字的高位
   注意2:"1"+s是在字符串s首加了1
*/
using namespace std;  
string rev(string s){
  reverse(s.begin(),s.end());
  return s;
}
string add(string s1,string s2){//大整数加法
  string s=s1;
  int carry=0;
  for(int i=s1.size()-1;i>=0;i--){
    s[i]=(s1[i]-'0'+s2[i]-'0'+carry)%10+'0';
    carry=(s1[i]-'0'+s2[i]-'0'+carry)/10;
  }
  if(carry>0)  s="1"+s;
  return s;
}
int main(){   
  string s,sum;
  int n=10;
  cin>>s;
  if(s==rev(s)){
    cout<<s<<" is a palindromic number.\n";
    return 0;
  }
  while(n--){
    sum=add(s,rev(s));
    cout<<s<<" + "<<rev(s)<<" = "<<sum<<endl;
    if(sum==rev(sum)){
      cout<<sum<<" is a palindromic number.\n";
      return 0;
    }
    s=sum;
  }
  cout <<"Not found in 10 iterations.\n";
  //system("pause");
    return 0;   
}
相关文章
|
11月前
|
机器学习/深度学习
1361:产生数(Produce)
1361:产生数(Produce)
|
索引
LeetCode 416. Partition Equal Subset Sum
给定一个只包含正整数的非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
84 0
LeetCode 416. Partition Equal Subset Sum
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
63 0
LeetCode 436. Find Right Interval
|
人工智能 索引
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
【1019】General Palindromic Number (20 分)
【1019】General Palindromic Number (20 分) 【1019】General Palindromic Number (20 分)
80 0
【1119】Pre- and Post-order Traversals (30分)
【1119】Pre- and Post-order Traversals (30分)
95 0
【1113】Integer Set Partition (25分)
【1113】Integer Set Partition (25分) 【1113】Integer Set Partition (25分)
88 0
1136. A Delayed Palindrome (20)
#include #include #include using namespace std; string s, s0; void add(string &s, string &s0){ int h = 0; for (int i = (int)s.
903 0
1104. Sum of Number Segments (20) consecutive subsequence 每个数出现的次数
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence.
946 0