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 ...
随机推荐
- poj1821——Fence
题意: 一个栅栏一共有n(从1--n)个木板,我们找k个工人去粉刷它,li表示每个人有限制粉刷木板数量,pi表示粉刷一个木板得到的钱,si表示他开始在那个木板前面 如果一个工人要粉刷,那么他必须粉刷s ...
- Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)
题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...
- 列表解析式 -- Python
列表解析的语法:[expr for iter_var in iterable], 它迭代iterable对象的所有条目.其中的expr应用于序列的每个成员,最后的结果值是该表达式产生的列表,迭代变量并 ...
- OpenStack Train版-15.创建并挂载存储卷
1.创建并挂载存储卷 创建一个1GB的卷 source ~/demo-openrc openstack volume create --size 1 volume1 很短的时间后,卷状态应该从crea ...
- 宝塔面板&FLASK¢os 7.2 &腾讯云 配置网站出现的若干问题
1.解决跨域问题&&中文显示问题 from flask import Flask, url_for, request, render_template, redirect from f ...
- 在Python里,用股票案例讲描述性统计分析方法(内容来自我的书)
描述性统计是数学统计分析里的一种方法,通过这种统计方法,能分析出数据整体状况以及数据间的关联.在这部分里,将用股票数据为样本,以matplotlib类为可视化工具,讲述描述性统计里常用指标的计算方法和 ...
- Linux 驱动框架---linux 驱动
总述 Linux 系统下的驱动最后都是以如下这个结构体呈现在系统中的,注意其中的dev_pm_ops是内核新增的内容来准备替换platform_driver中的电源管理相关的内容.这里内容是先进行总体 ...
- export html to canvas image
export html to canvas image canvas.toDataURL https://developer.mozilla.org/en-US/docs/Web/API/HTMLCa ...
- Kotlin 入门教程
Kotlin 入门教程 Android / Java https://developer.android.com/kotlin?hl=zh-cn 使用 Kotlin 开发 Android 应用 使用 ...
- 一个模块如何同时支持 ESM 和 CJS
一个模块如何同时支持 ESM 和 CJS 模块转化 webpack + babel refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问 ...