当前位置:网站首页>1024 Palindromic Number
1024 Palindromic Number
2022-06-27 19:50:00 【Brosto_ Cloud】
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
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. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤1010) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.
Output Specification:
For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.
Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3High precision addition + Palindrome number , It is necessary to judge whether the palindrome number is entered :
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int k, cnt = 0;
string s, ss;
cin >> s >> k;
ss = s;
bool flag = 1;
int j = 0;
for (int i = 0; i <= s.size() / 2; i++) {
if (s[i] != s[s.size() - i - 1]) {
flag = 0;
}
}
if (flag) {
cout << s << endl;
cout << 0;
return 0;
}
while (true) {
cnt++;
int t = 0;
for (int i = 0; i < ss.size(); i++) {
if (ss[i] != '0') {
t = i;
break;
}
}
s = "";
for (int i = t; i < ss.size(); i++) {
s += ss[i];
}
ss = s;
reverse(s.begin(), s.end());
s = '0' + s;
ss = '0' + ss;
for (int i = s.size() - 1; i >= 1; i--) {
int x = s[i] + ss[i] - '0' - '0';
ss[i] = '0' + x % 10;
ss[i - 1] += x / 10;
}
for (int i = 0; i < ss.size(); i++) {
if (ss[i] != '0') {
j = i;
break;
}
}
flag = 1;
int left = j, right = ss.size() - 1;
while (left <= right) {
if (ss[left] != ss[right]) {
flag = 0;
break;
}
left++;
right--;
}
if (cnt == k || flag) {
break;
}
}
for (int i = j; i < ss.size(); i++) {
cout << ss[i];
}
cout << endl;
if (flag) {
cout << cnt;
} else {
cout << k;
}
return 0;
}
边栏推荐
- 1023 Have Fun with Numbers
- Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
- shell脚本常用命令(四)
- PCB线路板蛇形布线要注意哪些问题?
- Tupu digital twin intelligent energy integrated management and control platform
- CMS 执行的七个阶段
- Labelimg usage guide
- 券商经理的开户二维码开户买股票安全吗?有谁知道啊
- (LC)46. 全排列
- 1025 PAT Ranking
猜你喜欢
随机推荐
GIS remote sensing R language learning see here
NVIDIA Clara-AGX-Developer-Kit installation
OpenSSL client programming: SSL session failure caused by an obscure function
CMS 执行的七个阶段
1027 Colors in Mars
广发期货开户安全吗?
Is the account opening QR code given by CICC securities manager safe? Who can I open an account with?
MySQL表的增删改查(基础)
Summary of submarine cable detection technology
UE4-Actor基础知识
多伦多大学博士论文 | 深度学习中的训练效率和鲁棒性
ABAP-CL_OBJECT_COLLECTION工具类
Pyhton爬取百度文库文字写入word文档
The Fifth Discipline: the art and practice of learning organization
Determine whether a variable is an array or an object?
判断一个变量是数组还是对象?
惊呆!原来 markdown 的画图功能如此强大!
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
如何封装调用一个库








