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