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 ...
随机推荐
- [模板]PAM
模板\([luogu5496]\) 题目 #include<bits/stdc++.h> using namespace std; const int N = 5e5 + 10; char ...
- HashMap 介绍
基本介绍 1. 用于存储Key-Value键值对的集合(每一个键值对也叫做一个Entry)(无顺序). 2. 根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值. 3. 键key为n ...
- thinkphp 模块部署
3.2对模块的访问是自动判断的,所以通常情况下无需配置模块列表即可访问,在部署模块的时候,默认情况下都是基于类似于子目录的URL方式来访问模块的,例如: http://serverName/Home/ ...
- 42 github 开源代码 ——README.md语法/相关操作等
0 引言 最近在github上开源了pro/E二次开发的代码,发现README.md的编辑方式很有趣,需要稍微了解一下. 1 markdown语法 参考了两篇博客的内容,链接如下. https://b ...
- delphi实现圆角窗体[转]
procedure TForm1.FormCreate(Sender: TObject); var hr :thandle; begin hr:=createroundrectrgn(1,1,widt ...
- hive的行列互转
行转列 多行转多列 数据表 row2col col1 col2 col3 a c 1 a d 2 a e 3 b c 4 b d 5 b e 6 现在要将其转化为: col1 c d e a 1 2 ...
- NX二次开发-UFUN将建模绝对空间中的点映射到工程图坐标UF_VIEW_map_model_to_drawing
#include <uf.h> #include <uf_ui.h> #include <uf_draw.h> #include <uf_view.h> ...
- fasttext的基本使用 java 、python为例子
fasttext的基本使用 java .python为例子 今天早上在地铁上看到知乎上看到有人使用fasttext进行文本分类,到公司试了下情况在GitHub上找了下,最开始是c++版本的实现,不过有 ...
- 剑指offer——19删除链表的节点
题目一: 在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 书本讲得不明就里 class Solution { void DeleteNode ...
- python中与time模块和datetime模块相关操作
使用time模块计时程序运行时长 import time time_start = time.time() #程序主体部分 time_end = time.time() print('总耗时: ', ...