A Delayed Palindrome

PAT-1136

  • 我这里将数字转换为字符串使用的是stringstream字符串流
  • 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
bool ispalindromic(string s){
int len=s.length();
for(int i=0;i<len/2;i++){
if(s[i]!=s[len-i-1])
return false;
}
return true;
}
string add(string first,string last){
reverse(first.begin(),first.end());
reverse(last.begin(),last.end());
string total="";
int c=0;
for(int i=0;i<first.length();i++){
int a=first[i]-'0';
int b=last[i]-'0';
int temp=a+b+c;
total+=((temp%10)+'0');
c=temp/10;
}
if(c!=0){
stringstream now;
now<<c;
string tempc=now.str();
reverse(tempc.begin(),tempc.end());
total+=tempc;
}
reverse(total.begin(),total.end());
return total;
}
int main() {
string s;
cin>>s;
int len=s.length();
string original=s;
if(ispalindromic(original)){
cout<<original<<" is a palindromic number.";
return 0;
}
for(int i=0;i<10;i++){
string temp=original;
string temp1=original;
reverse(temp1.begin(),temp1.end());
original=add(temp,temp1);
cout<<temp<<" + "<<temp1<<" = "<<original<<endl;
if(ispalindromic(original)){
cout<<original<<" is a palindromic number.";
return 0;
}
}
cout<<"Not found in 10 iterations."<<endl;
return 0;
}

最新文章

  1. 牛客网程序员面试金典:1.2——原串翻转(java实现)
  2. java 中的volatile
  3. VendorNPC.lua --随身商人
  4. jquery选择器(原创)&lt;四&gt;
  5. iOS--九宫格奥秘(UIView)(arc4random)
  6. 2015ACM/ICPC亚洲区长春站 A hdu 5527 Too Rich
  7. IIS7/8 HTTP Error 500.19 错误 0x80070021
  8. C#操作Excel文件(转)
  9. Unable to open log device &#39;/dev/log/main&#39;: No such file or directory
  10. Python标准库简介
  11. Sql数据类型转换
  12. 计算机程序的思维逻辑 (95) - Java 8的日期和时间API
  13. Linux忘记开机密码怎么办?
  14. dnmp(docker的lnmp)安装WordPress之后图片上传问题 问题:图片上传大小问题解决和 报错413 Request Entity Too Large
  15. java的四种内部类详解
  16. day 7-12 数据库的基本操作和存储引擎
  17. 往github上传代码忽略node_modules文件夹
  18. 12306登录爬虫 cookies版本
  19. #python计算结果百位500向下取整,(0-499取000,500-999取500)
  20. Crack相关

热门文章

  1. KMP(算法描述)
  2. python连接mysql数据库,并进行添加、查找数据
  3. Excel添加超链接
  4. 3.Work Queues
  5. HDU 2825 Wireless Password(AC自动机 + 状压DP)题解
  6. xss之htmlspecialchars
  7. Redis-第八章节-应用场景
  8. TensorFlow+restore读取模型
  9. queueMicrotask &amp; microtask
  10. c++指针练习