PAT甲级——A1104 Sum of Number Segments【20】
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 <string>
#include <algorithm>
using namespace std;
int main()
{
int k = ;
string str1, str2, str;
cin >> str;
while (k < )
{
int c = ;
str1 = str2 = str;
reverse(str2.begin(), str2.end());
if (str1 == str2)
break;
cout << str1 << " + " << str2 << " = ";
for (int i = str1.length()-; i>=; --i)
{
str[i] = (str1[i] - '' + str2[i] - '' + c) % + '';
c = (str1[i] - '' + str2[i] - '' + c) / ;
}
if (c > )
str.insert(str.begin(), , c + '');
cout << str << endl;
k++;
}
if (k == )
cout << "Not found in 10 iterations." << endl;
else
cout << str1 << " is a palindromic number." << endl;
return ;
}
PAT甲级——A1104 Sum of Number Segments【20】的更多相关文章
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT Advanced A1104 Sum of Number Segments (20) [数学问题]
题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...
- PAT 甲级 1104. Sum of Number Segments (20) 【数学】
题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...
- PAT 甲级 1104 sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
- PAT 甲级 1019 General Palindromic Number(20)(测试点分析)
1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...
- PAT A1104 Sum of Number Segments (20 分)——数学规律,long long
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- 【PAT甲级】1104 Sum of Number Segments (20 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- PAT (Advanced Level) 1104. Sum of Number Segments (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- 移动端使用fastclick 解决
html vue 1. cnpm i fastclick --save 2. 在main.js中引入并绑定到body import fastclick from 'fastclick'; 3. fas ...
- flyway 管理数据库版本
Flyway 和 Liquibase 都是 Java 项目中常用的 DB migration 工具, 从使用简便性看,Flyway 比 Liquibase 更简单, 从 github 的 star 数 ...
- 【转载】带你吃透RTMP
RTMP协议是Real Time Message Protocol(实时信息传输协议)的缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing) ...
- Kafka Streams详细
概述 1 Kafka Streams Kafka Streams.Apache Kafka开源项目的一个组成部分.是一个功能强大,易于使用的库.用于在Kafka上构建高可分布式.拓展性,容错的应用程序 ...
- LUOGU P3723 [AH2017/HNOI2017]礼物 (fft)
传送门 解题思路 首先我们设变化量为\(r\),那么最终的答案就可以写成 : \[ ans=min(\sum\limits_{i=1}^n(a_i-b_i+r)^2) \] \[ ans=min(\s ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- sqlserver中常用的windows命令行的操作
1.删除指定目录下指定时间之前的文件: ), ), @sqltxtdel varchar(max) --指定的删除时间 set @deldate= '-8' --指定的删除路径 set @bakpat ...
- [转] boost undefined reference to 'pthread_create 问题
由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...
- 基于Netty的RPC架构学习笔记(十一):粘包、分包分析,如何避免socket攻击
文章目录 问题 消息如何在管道中流转 源码解析 AbstractNioSelector.java AbstractNioWorker.java NioWorker.java DefaultChanne ...
- mave打包spring项目成jar,启动报错:Offending resource: URL [
文章目录 错误 解决 错误 Offending resource: URL [jar:file:/Users/leesin/intellij-idea-workspace/rabbitMq_Consu ...