A1136. Delayed Palindrome
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.
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct NODE{
int num[], len;
NODE(){
fill(num, num + , );
len = ;
}
}bign;
void add(bign &a, bign &b, bign &c){
c.len = ;
b.len = ;
for(int i = a.len - ; i >= ; i--){
b.num[b.len++] = a.num[i];
}
int carry = ;
int i;
for(i = ; i < b.len && i < a.len; i++){
int sum = a.num[i] + b.num[i] + carry;
carry = sum / ;
c.num[c.len++] = sum % ;
}
while(i < b.len){
int sum = carry + b.num[i];
c.num[c.len++] = sum % ;
carry = sum / ;
}
while(i < a.len){
int sum = carry + a.num[i];
c.num[c.len++] = sum % ;
carry = sum / ;
}
if(carry != ){
c.num[c.len++] = carry;
}
}
int isReverse(bign a){
for(int i = , j = a.len - ; i <= j; i++, j--){
if(a.num[i] != a.num[j])
return ;
}
return ;
}
int main(){
char ss[];
scanf("%s", ss);
bign a, b, c;
for(int i = strlen(ss) - ; i >= ; i--){
a.num[a.len++] = ss[i] - '';
}
int tag = ;
if(isReverse(a)){
for(int i = a.len - ; i >= ; i--){
printf("%d", a.num[i]);
}
printf(" is a palindromic number.");
return ;
}
for(int i = ; i < ; i++){
add(a,b,c);
for(int k = a.len - ; k >= ; k--){
printf("%d", a.num[k]);
}
printf(" + ");
for(int k = b.len - ; k >= ; k--){
printf("%d", b.num[k]);
}
printf(" = ");
for(int k = c.len - ; k >= ; k--){
printf("%d", c.num[k]);
}
printf("\n");
if(isReverse(c)){
tag = ;
for(int j = c.len - ; j >= ; j--){
printf("%d", c.num[j]);
}
printf(" is a palindromic number.");
break;
}else{
for(int k = ; k < c.len; k++){
a.num[k] = c.num[k];
}
a.len = c.len;
}
}
if(tag == ){
printf("Not found in 10 iterations.\n");
}
cin >> ss;
return ;
}
总结:
1、大整数相加的问题。要注意的是,在a+b做完之后,要注意检查carry是否为0,如果不为0的话,需要再把carry加上。
2、1230的相反是0123而不是123,所以本题相当于两个待加的数字位数都相同,所以可以直接用两个string做加法,比使用大整数模拟要快一些。
A1136. Delayed Palindrome的更多相关文章
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT1136:A Delayed Palindrome
1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1136 A Delayed Palindrome
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 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 ...
- PAT_A1136#A Delayed Palindrome
Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...
- 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)字符串处理+字符串和数字间的转换
A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...
随机推荐
- 剑指offer(16)栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...
- 关于 ajax
1.type 提交类型 get /post 2.async 默认true 异步 3.cache 默认 true 读取缓存 false不读取缓存 会在请求后面 添加一个时间戳 https://www. ...
- Gatsby & React & NPX & NVM
Gatsby & React Gatsby is a blazing fast modern site generator for React. https://www.gatsbyjs.or ...
- Windows下安装Ubuntu 16.04双系统
本文已有更新:新文章 [2016-05-09 更新说明: ①:我原本写的Ubuntu 16.04安装博客中在安装系统时,在引导项部分,有一点问题没有注意到,感谢@小段阿誉的指出,在下面我有了说明: ② ...
- 动态追加js
判断是否已引用js,如果没有会引发异常,在异常时添加引用 try { if (layui) {} } catch (ex) { var s = document.createElement('scri ...
- 19JDBC初体验
一.JDBC常用类和接口 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API.JDBC是Java访问数据库的标准规范,可以为 ...
- fastjson的JSONArray和JSONObject
转自: http://blog.csdn.net/tangerr/article/details/76217924 Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理 ...
- kubernetes增加污点,达到pod是否能在做节点运行
master node参与工作负载 (只在主节点执行)使用kubeadm初始化的集群,出于安全考虑Pod不会被调度到Master Node上,也就是说Master Node不参与工作负载. 这里搭建的 ...
- POJ 3322 Bloxorz(算竞进阶习题)
bfs 标准广搜题,主要是把每一步可能的坐标都先预处理出来,会好写很多 每个状态对应三个限制条件,x坐标.y坐标.lie=0表示直立在(x,y),lie=1表示横着躺,左半边在(x,y),lie=2表 ...
- hdu 1540 Tunnel Warfare(Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 思路:三种操作: D摧毁一个点 R重建最晚被修改的那个点 Q询问点x联通的点有多少个 逆向思维,D操 ...