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 ...
随机推荐
- 杜教BM【转载】
https://blog.csdn.net/qq_36876305/article/details/80275708 #include <bits/stdc++.h> using name ...
- poj 2155 B - Matrix 二维树状数组
#include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...
- Tag文件的创建与应用
Tag文件,几乎和JSP文件一模一样,可以被JSP页面动态加载调用.Tag文件有什么优势呢(既然和JSP几乎一模一样,那就得想想这个必然有不一样的地方,不然要它存在干嘛) 在设计Web应用时,可以通过 ...
- MySQL--批量KILL连接
============================================== 使用SELECT INTO OUTFILE方式获取到要删除的连接ID并保存为文件,在通过SOURCE执行 ...
- Scala中的Map使用例子
Map结构是一种非常常见的结构,在各种程序语言都有对应的api,由于Spark的底层语言是Scala,所以有必要来了解下Scala中的Map使用方法. (1)不可变Map特点: api不太丰富 如果是 ...
- 曾经很强大的免费 ERP 2BizBox
曾经很强大的免费 ERP 2BizBox 整个功能很强大,特别是生产,工单也很完善,有损耗,有反冲等功能. 流程比较规矩,需要先采购后才能使用,只有生产后才能销售,工单有组装和拆装,工程有工程更改,也 ...
- WebSocket概念
WebSocket 是什么? WebSocket 是一种网络通信协议.RFC6455 定义了它的通信标准. WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议 ...
- flume简介
组件介绍: 代理 Flume Agent Flume内部有一个或者多个Agent 每一个Agent是一个独立的守护进程(JVM) 从客户端哪儿接收收集,或者从其他的Agent哪儿接收,然后迅速的将获取 ...
- 读取配置文件的C语言接口实现
在一些场合,需要对一些配置文件进行读取,去设置软件的参数,自己实现了一些接口函数,以供以后使用. ConfigFile.c #include <stdio.h> #include < ...
- idea补丁破解
1.下载JetbrainsCrack-2.9-release-enc.jar破解补丁,下载地址:http://idea.lanyus.com 2.将其放在你的安装 idea下面的根目录下面 3.修改根 ...