PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0≤ai<10 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.
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
string add(string s1,string s2){
string res="";
int carry=;
for(int i=s1.length()-;i>=;i--){
int tmp=s2[i]-''+s1[i]-''+carry;
res+=(tmp%+'');
carry=tmp/;
}
if(carry!=)res += (carry+'');
reverse(res.begin(),res.end());
return res;
}
bool ispali(string s){
string s2;
s2=s;
reverse(s2.begin(),s2.end());
if(s==s2) return true;
else return false;
}
int main(){
string s1,s2,s3;
cin>>s1;
int i;
if(ispali(s1)){
cout<<s1<<" is a palindromic number."<<endl;
return ;
}
for(i=;i<;i++){
s2=s1;
reverse(s2.begin(),s2.end());
s3=add(s1,s2);
cout<<s1<<" + "<<s2<<" = "<<s3<<endl;
if(ispali(s3)){
cout<<s3<<" is a palindromic number."<<endl;
return ;
}
s1=s3;
}
printf("Not found in 10 iterations.\n");
}
注意点:判断回文用string还是方便,string只能string+char,不能char+string,所以大数相加还是要最后反转一下。
PAT A1136 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乙级:1088 三人行 (20分)
PAT乙级:1088 三人行 (20分) 题干 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整 ...
- PAT乙级:1064 朋友数 (20分)
PAT乙级:1064 朋友数 (20分) 题干 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 ...
- 【CF932G】Palindrome Partition(回文树,动态规划)
[CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
随机推荐
- mongodb 权限设置--用户名、密码、端口
转自:http://www.cnblogs.com/valor-xh/p/6369432.html 一.关于权限的默认配置 在默认情况下,mongod是监听在0.0.0.0之上的,任何客户端都可以直接 ...
- CDN使用心得:加速双刃剑
文章图片存储在GitHub,网速不佳的朋友,请看<CDN 使用心得:加速双刃剑> 或者 来我的技术小站 godbmw.com 本文以腾讯云平台的 CDN 服务为例,记录下在个人网站开发和公 ...
- PHP7.27: object
http://www.devshed.com/c/a/PHP/PHP-Services-Layers-Data-Mappers/ https://stackoverflow.com/questions ...
- JS之iscroll.js的使用详解
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- flex 布局下,css 设置文本不换行时,省略号不显示的解决办法
大致是有一个 main 容器是 flex 布局,左边一个 logo 固定宽高,右边 content 动态宽度. <div class="main"> <img a ...
- 项目启动时发生NOT found
一直想记录一下这个小问题 情景: 我昨晚美滋滋的做完功能,测了测没bug提交到git上之后就屁颠屁颠的回家了,结果今天早上来就失了智,git pull拉了一下代码后,一运行,我去,我的页面呢,页面上直 ...
- python之数据类型
1.整数(int)integer 直接写出数字就是整数例: a = 0#查看变量的数据类型 type() -> #<class 'int'> class类,类型,类别print(10 ...
- SAP 官网中文帮助文件&BP中文资料汇总
系统 描述 版本 连接 SAP ME 制造执行 SAP Manufacturing Execution (SAP ME) 15.0 点击我 SAP ECC EHP6 财务部分 SAP ERP 6.0 ...
- JavaScript大杂烩5 - JavaScript对象的若干问题
1. 类型检查:instanceof与typeof 这是两个相似的操作符,instanceof用于检测函数的实例类型,主要是在面向对象编程中检查new出来的对象类型,需要注意instanceof是检查 ...
- 服务器 nginx配置 防止其他域名绑定自己的服务器
基于我的网站被其他的域名恶意绑定了,我做出了如下处理,全站转https,同时配置nginx跳转禁止其他绑定ip的域名访问(原理主机空域名可绑定任意的,参考https://www.jb51.net/ar ...