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 ...
随机推荐
- 实现tail
编程之路刚刚开始,错误难免,希望大家能够指出. 自己实现一个tail的功能(使用IO系统调用),完全类似的操作步骤就不实现了,主要是让自己加深了解. 下面的代码不足之处很多,以后有空改正. #incl ...
- web.xml配置Servlet出错(DispatcherServlet)
想当然的把servlet-mapping当成servlet的属性 后台也不报错,但是输入url,就是死活不执行那个servlet servlet和servlet-mapping是同一级的对象
- src-d engine 强大的git 历史分析工具
src-d engine 包含了Babelfish ,同时可以看作是Babelfish 的升级版本,我们可以使用sql 进行代码分析 备注: 注意运行的时候需要容器支持 使用 安装docker 参 ...
- 使用 --image-repository 解决kubeadm 安装k8s 集群 谷歌镜像墙的问题
从网上我们看到的好多kubeadm 安装k8s 的时候都说需要下拉取镜像,然后修改,实际上 我们可以使用配置参数,快速的跳过墙的问题 说明: 基础镜像,我们仍然存在,拉取的问题,但是dockerhub ...
- centos配置apache的https服务
因为公司要开发微信小程序,由于小程序比较特殊,需要https服务,所以就研究了下apache的https服务了,大致过程如下: 1.向证书机构申请https证书,会得到证书和私钥 2.安装apache ...
- Computer Go Programming 学习
https://senseis.xmp.net/?ComputerGoProgramming https://www.worldscientific.com/doi/pdf/10.1142/S0218 ...
- Python_TCP/IP简介
本篇将开始介绍Python的网络编程,更多内容请参考:Python学习指南 自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接在一起,让网络中 ...
- 应用端连接MySQL数据库报Communications link failure
事情的起因: 某项目的开发同学突然Q我们组的某同学,要求我们调整MySQL的连接等待超时参数wait_timeout.要求我们从28800s调整到31536000s(也就是一年) 应用端测试环境的to ...
- SQL数据库简单操作
sql语言简介 (1)数据库是文件系统,使用标准sql对数据库进行操作 * 标准sql,在mysql里面使用语句,在oracle.db2都可以使用这个语句 (2)什么是sql * Structured ...
- C166-变量和函数指定物理地址一
1.函数指定地址 按照http://www.keil.com/support/docs/2215.htm C166: LOCATING PROGRAM SECTIONS TO SPECIAL MEMO ...