PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换
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;
}
PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换的更多相关文章
- PAT 1136 A Delayed Palindrome
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k ...
- PAT 1136 A Delayed Palindrome[简单]
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...
- pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome
题意:略. 思路:大整数相加,回文数判断.对首次输入的数也要判断其是否是回文数,故这里用do...while,而不用while. 代码: #include <iostream> #incl ...
- PAT1136:A Delayed Palindrome
1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- 西南民族大学第十二届程序设计竞赛(同步赛) A.逃出机房 (bfs)
题意:有来两个人A和B,A追B,A和B每次向上下左右移动一个单位,一共有两扇门,问A是否可以追上B(在门口追上也算合法). 题解:当时看题意说在门口也算?就觉得是判断两个人到门口的时间,对他们两个人分 ...
- Miller_Rabbin算法判断大素数
普通的素数测试我们有O(√ n)的试除算法.事实上,我们有O(s*log³n)的算法. 下面就介绍一下Miller_Rabbin算法思想: 定理一:假如p是质数,且(a,p)=1,那么a^(p-1)≡ ...
- JavaScript——二
样式: 实验二. querySelectorAll()里面如果填id名称就直接写,如果要确定某个属性的值,就要用到[ ]来具体选择,其中写多个以空格隔开就表达第一个声明下的第二个标签内部的某个属性 这 ...
- .net core 更换yum源 / “No package libgdiplus-devel available.” 错误解决方法
安装 libgdiplus-devel yum install libgdiplus-devel 如果出现错误 No package libgdiplus-devel available. 原因可能是 ...
- div 水平居中 内容居左
<div style="margin:0 auto;width:500px;text-align:left"> </div> https://zhidao. ...
- 4.PowerShell DSC核心概念之配置
什么是配置 DSC 配置是定义某一特殊类型函数的 PowerShell 脚本. 配置的语法 Configuration MyDscConfiguration { #配置块 Import-DscReso ...
- ThreadLocal使用全解
一.何为ThreadLocal 1.ThreadLocal的含义 ThreadLocal,即线程变量,是一个以ThreadLocal对象为键,任意对象为值的存储结构.这个结构被附带在线程上,也就是说一 ...
- 实战交付一套dubbo微服务到k8s集群(4)之dubbo微服务底包镜像制作
1.下载jre镜像 在运维主机(mfyxw50.mfyxw.com)操作 [root@mfyxw50 ~]# docker pull registry.cn-hangzhou.aliyuncs.com ...
- 实战交付一套dubbo微服务到k8s集群(2)之Jenkins部署
Jenkins官网:https://www.jenkins.io/zh/ Jenkins 2.190.3 镜像地址:docker pull jenkins/jenkins:2.190.3 1.下载Je ...
- 二进制安装kubernetes(六) kube-proxy组件安装
Kube-Proxy简述 参考文献: https://ywnz.com/linuxyffq/2530.html 运行在每个节点上,监听 API Server 中服务对象的变化,再通过管理 IPtabl ...