PAT_A1136#A Delayed Palindrome
Source:
Description:
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 reversedA
, andC
is their sum.A
starts being the input number, and this process ends untilC
becomes a palindromic number -- in this case we print in the last lineC is a palindromic number.
; or if a palindromic number cannot be found in 10 iterations, printNot 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.
Keys:
- 快乐模拟
Attention:
- under algorithm, reverse(s.begin(),s.end());
Code:
/*
Data: 2019-08-07 19:32:34
Problem: PAT_A1136#A Delayed Palindrome
AC: 17:12 题目大意:
非回文数转化为回文数;
while(! palindrome){
1.Reverse
2.add
输入:
给一个不超过1000位的正整数
输出:
给出每次循环的加法操作,最多10次循环
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; bool IsPali(string s)
{
int len=s.size();
for(int i=; i<len/; i++)
if(s[i] != s[len--i])
return false;
return true;
} string Func(string s1)
{
string s,s2=s1;
reverse(s2.begin(),s2.end());
int carry=;
for(int i=; i<s1.size(); i++)
{
carry += (s1[i]-''+s2[i]-'');
s.insert(s.end(),''+carry%);
carry /= ;
}
while(carry!=)
{
s.insert(s.end(),''+carry%);
carry /= ;
}
reverse(s.begin(),s.end());
printf("%s + %s = %s\n", s1.c_str(),s2.c_str(),s.c_str());
return s;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE string s;
cin >> s;
for(int i=; i<; i++)
{
if(IsPali(s))
{
printf("%s is a palindromic number.\n", s.c_str());
s.clear();break;
}
s = Func(s);
}
if(s.size())
printf("Not found in 10 iterations."); return ;
}
最新文章
- MYSQL远程登录权限设置 ,可以让Navicat远程连接服务器的数据库
- mysql深入浅出的笔记(存储过程一)
- BFC之浅析篇
- Linux系统性能分析
- Microsoft Azure 01 (Summarize)
- Mysql在高并发情况下,防止库存超卖而小于0的解决方案
- pl/sql developer——instant-client 简单配置
- (Python)集合、集合的函数
- 内部通信服务Factory(WCF)
- 【转】 ios开发之倒计时实现的两种方法
- C#实现下载功能,可用于软件自动更新
- SQL整理5
- 微型orm框架--dapper的简单使用
- mobiscroll2.5.4 日期组件
- github---无命令可视化界面操作
- MyBatis - 10.MyBatis扩展
- 【Java基础】10、Java中throw和throws的区别
- ***PHP基于H5的微信支付开发详解(CI框架)
- cors跨域问题
- 一个非常有意思的蜜罐T-Pot 16.10