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 ...
随机推荐
- What is The Rule of Three?
Question: What does copying an object mean? What are the copy constructor and the copy assignment op ...
- 详解scss的继承、占位符和混合宏
1.继承和占位符 两者都是通过@extend来引用. 1.1 继承 一个已经存在的css样式类,可以被其他样式类继承. 例如,实现以下css样式: .btn, .btn--primary, .btn- ...
- linq使用Take和Skip实现分页
;//第1页 ;//页大小 var list = list.Skip((pageIndex-1) * pageSize).Take(pageSize).ToList();
- polyfill-eventsource added missing EventSource to window ie浏览器 解决方案
今天遇到一个 ie浏览器显示空白,报错内容是: polyfill-eventsource added missing EventSource to window的问题, import 'babel-p ...
- 【代码笔记】Web-Javascript-JavaScript简介
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- api接口签名认证的一种方式
请求方 try { using (var client = new HttpClient()) { StringContent content = new StringContent(strParam ...
- Linux 学习笔记之超详细基础linux命令 Part 1
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 说明:主要是在REHL Server 6操作系统下进行的测试 --字符界面虚拟终端与图形界面之间的切 方法:[ ...
- tomcat 取消项目名访问路径
在server.xml 里,<host>...</host>的标签之间添加<Context path="" docBase="projec ...
- Kotlin入门(13)类成员的众生相
上一篇文章介绍了类的简单定义及其构造方式,当时为了方便观察演示结果,在示例代码的构造函数中直接调用toast提示方法,但实际开发是不能这么干的.合理的做法是外部访问类的成员属性或者成员方法,从而获得处 ...
- recovery uncrypt功能解析(bootable/recovery/uncrypt/uncrypt.cpp)
我们通常对一个文件可以直接读写操作,或者普通的分区(没有文件系统)也是一样,直接对/dev/block/boot直接读写,就可以获取里面的数据内容了. 当我们在ota升级的时候,把升级包下载到cach ...