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 ...
随机推荐
- Angular 自定义过滤器
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- C# Note21: 扩展方法(Extension Method)及其应用
前言 今天在开会时提到的一个概念,入职3个多月多注重在项目中使用C#的编程知识,一直没有很认真地过一遍C#的全部语法,当我们新人被问及是否了解Extension Method时,一时之间竟不能很通俗准 ...
- NOIP2015提高组复赛B 子串
题目链接:https://ac.nowcoder.com/acm/contest/263/B 题目大意: 略 分析: 设preA(i)为字符串A中第1个字符到第i个字符构成的字符串. 设preB(i) ...
- 排查 Maxwell can not find database 并且使用 MySQL binlog 解决相关问题
目前我们在使用 Maxwell 在读线上机器的 binlog 同步我们的离线数据库. 这次错误定位上,首先线要确定问题是发生在生产者 还是队列 还是消费者.经过查看各机器上任务的运行日志,定位到了问题 ...
- gitlab+jenkins
一.安装好gitlab.jenkins yum install -y java wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-sta ...
- 关于WPF中Popup中的一些用法的总结
Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧: System.Object ...
- StringBuilder与String有哪些区别?
System.String具备不可修改性,在程序中这样的特性容易产生性能上的问题.针对这个问题.NET提供的StringBuilder类可以解决类似的问题. String 和 StringBuilde ...
- 错误:org.apache.catalina.LifecycleException: Protocol handler start failed
org.apache.catalina.LifecycleException: Protocol handler start failed at org.apache.catalina.connect ...
- easyui datagrid动态修改editor时动态绑定combobox的数据
需求在 datagrid 编辑框中开启一个combobox ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...
- 如何调用layer.open打开的的iframe窗口中的JS