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 ...
- 浅谈基于Prism的软件系统的架构设计
很早就想写这么一篇文章来对近几年使用Prism框架来设计软件来做一次深入的分析了,但直到最近才开始整理,说到软件系统的设计这里面有太多的学问,只有经过大量的探索才能够设计出好的软件产品,就本人的理解, ...
- 思维导图,UML图,程序流程图制作从入门到精通
工具: https://www.processon.com/ 第一 用例图 第二 时序图 第三 流程图
- js正則表達式
正則表達式實例化的兩種方式: 字符型 var a=// 對象型var a=new RegExp(,) 修飾符: i:忽略大小寫 g:全局搜索 m:多行搜索 元字符: \轉義字符 \w:字符,數字,下劃 ...
- easyui datagrid动态修改editor时动态绑定combobox的数据
需求在 datagrid 编辑框中开启一个combobox ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...
- Coalesce (MS SQL Server)——取指定内容(列)中第一个不为空的值
oalesce 获得参数中第一个不为空的表达式. 语法: COALESCE ( expression [ ,...n ] ) 例子:CREATE TABLE wages ...
- solr +IKAnalyzer2012FF_u1 功能图
- 在 Web 页面中使用离线地图
1. 所需工具&插件: 1. MapDownloader (提取码: spx6) 2. GISMysqlToLocalFile (提取码: vus6) 3. Leaflet 2. 操作: 1. ...
- 当页面上需要的字段不在model中时候,需要自行设置该字段
当页面上需要的字段不在model中时候,需要自行设置该字段
- Nginx tcp限制并发、IP、记日志
L:114 Syntax: limit_conn_zone key zone=name:size;//类似http limit_conn 需要开个共享内存 zone=name(共享内存名称):siz ...