1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0 for all i and ak>0. Then N is palindromic if and only if ai=ak−i for all i. Zero is written 0 and is also palindromic by definition.
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, you are supposed to find its paired palindromic number.
Input Specification:
Each input file contains one test case which gives a positive integer no more than 1000 digits.
Output Specification:
For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:
A + B = C
where A
is the original number, B
is the reversed A
, and C
is their sum. A
starts being the input number, and this process ends until C
becomes a palindromic number -- in this case we print in the last line C is a palindromic number.
; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations.
instead.
Sample Input 1:
97152
Sample Output 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
Sample Input 2:
196
Sample Output 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
分析:大数模拟,最后一个测试点应该是大数边界,因为数可能是1000位的,所以如果用int 和long long 都会超出范围。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; string rev(string s){ reverse(s.begin(),s.end()); return s; } string add(string s1,string s2){ ; string s=s1; ;i>=;i--){ '+carry; s[i]=temp%+'; carry=temp/; } ) s='+s; return s; } bool ispali(string s){ int len=s.length(); ;i<len;i++){ ]){ return false; } } return true; } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif string num,s; cin>>num; if(num==rev(num)){ cout<<num<<" is a palindromic number."; ; } ; bool flag=false; while(k--){ s=rev(num); string sum=add(s,num); flag=ispali(sum); cout<<num<<" + "<<s<<" = "<<sum<<endl; if(flag==true){ cout<<sum<<" is a palindromic number."; break; } num=sum; } ){ cout<<"Not found in 10 iterations."; } ; }
1136 A Delayed Palindrome (20 分)的更多相关文章
- 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 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(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
- PAT 1136 A Delayed Palindrome[简单]
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...
- 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 ...
- PAT_A1136#A Delayed Palindrome
Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
随机推荐
- time,datetime模块
time & datetime 模块 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面 ...
- 【Jmeter】插件
一.插件管理 前提:很多时候,尤其是性能测试的时候,我们需要用到很多场景,需要得到一些参数值等等. 二.插件地址 URL : https://jmeter-plugins.org/downloads/ ...
- day 59 Bootstrap学习
---恢复内容开始--- 简介 栅格系统用于通过一系列的行(row)与列(column)的组合来创建页面布局,你的内容就可以放入这些创建好的布局中.下面就介绍一下 Bootstrap 栅格系统的工作原 ...
- Singer 学习二 使用Singer进行gitlab 2 postgres 数据转换
Singer 可以方便的进行数据的etl 处理,我们可以处理的数据可以是api 接口,也可以是数据库数据,或者 是文件 备注: 测试使用docker-compose 运行&&提供数据库 ...
- 04C++const增强、枚举的增强
#include <iostream> int main(void) { //const定义常量--->const意味着只读 const int a; int const b; // ...
- Jmeter分离登录事务的另一种方式
最近,遇到了一个困扰很多人的问题. 情景如下: 业务流程:登录一个网站,反复进行充值. 通常的做法是使用jmeter对登录和充值的接口进行反复的执行: 但是实现的方法却不能完美的贴合业务流程. 并且, ...
- react-hot-loader 3.0于1.3的区别
现在react-hot-loader 3.0版本应该还是beta版本,不过没关系,还是可以正常使用,我在项目中用的是react-hot-loader 3.0.0-beta.7 版本,并没用发现任何问题 ...
- 自定义flume的hbase sink 的序列化程序
package com.hello.hbase; import java.nio.charset.Charset; import java.text.SimpleDateFormat; import ...
- sqoop上传数据到hdfs,并用hive管理数据。
sqoop导入mysql数据表到HDFS中sqoop import --connect jdbc:mysql://master:3306/test --username root --password ...
- VMWare虚拟机安装 - 学习者系列文章
以前笔者写过一个VMWare虚拟机的安装说明,但是那个版本比较低,已经过时,今天,讲讲VMWare的14版本的安装和使用说明. 首先,下载VMWare 14的安装文件,这里提供一下百度网盘文件: 链接 ...