当前位置:网站首页>1024 Palindromic Number
1024 Palindromic Number
2022-06-27 17:53: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
3高精加法+回文数,需要特判一下输入是不是回文数:
#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;
}
边栏推荐
- Error reported by Huada MCU Keil_ Weak's solution
- 在线文本按行批量反转工具
- Solution to Maxwell error (MySQL 8.x connection)
- 新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
- Blink SQL内置函数大全
- 清华徐勇、段文晖研究组开发出高效精确的第一性原理电子结构深度学习方法与程序
- Four years of College for an ordinary graduate
- Campus book resource sharing platform
- 惊呆!原来 markdown 的画图功能如此强大!
- Substrate及波卡一周技术更新速递 20220425 - 20220501
猜你喜欢

Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million

基于STM32F103ZET6库函数外部中断实验

GIS遥感R语言学习看这里

Introduction to deep learning and neural networks

External interrupt experiment based on stm32f103zet6 library function

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

GIS remote sensing R language learning see here

DCC888 :Register Allocation

DFS and BFS simple principle

别焦虑了,这才是中国各行业的工资真相
随机推荐
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
带你认识图数据库性能和场景测试利器LDBC SNB
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
基础数据类型和复杂数据类型
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
NVIDIA Clara-AGX-Developer-Kit installation
Cdga | what is the core of digital transformation in the transportation industry?
Market status and development prospect forecast of global handheld ventilator industry in 2022
通过 G1 GC Log 重新认识 G1 垃圾回收器
买股票在券商经理的开户链接上开户安全吗?求大神赐教
流程判断-三目运算-for循环
Introduction to deep learning and neural networks
Don't worry. This is the truth about wages in all industries in China
高收益银行理财产品在哪里看?
One week technical update express of substrate and Boca 20220425 - 20220501
图扑数字孪生智慧能源一体化管控平台
Buzzer experiment based on stm32f103zet6 library function
2022年第一季度消费金融APP用户洞察——总数达4479万人
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
One to one relationship