当前位置:网站首页>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;
}
边栏推荐
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
- Implementation of reliable distributed locks redlock and redisson
- 基于STM32F103ZET6库函数按键输入实验
- Comment encapsuler un appel à une bibliothèque
- 金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
- PCB线路板蛇形布线要注意哪些问题?
- Bit. Store: long bear market, stable stacking products may become the main theme
- 什么是 ICMP ?ping和ICMP之间有啥关系?
- Labelimg usage guide
- 谈谈线程安全
猜你喜欢
Introduction to deep learning and neural networks
A simple calculation method of vanishing point
Crawl national laws and Regulations Database
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
Cloud native database: the outlet of the database, you can also take off
crontab的学习随笔
GIS遥感R语言学习看这里
binder hwbinder vndbinder
指针和结构体
基于STM32F103ZET6库函数跑马灯实验
随机推荐
Memoirs of actual combat: breaking the border from webshell
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
Market status and development prospect forecast of global aircraft hose industry in 2022
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
Is it safe to buy stocks and open an account on the account opening link of the securities manager? Ask the great God for help
Redis 原理 - String
OpenSSL client programming: SSL session failure caused by an obscure function
一种朴素的消失点计算方法
中金证券经理给的开户二维码安全吗?找谁可以开户啊?
binder hwbinder vndbinder
RANSAC的代码和原理
基于STM32F103ZET6库函数跑马灯实验
SQL Server - Window Function - 解决连续N条记录过滤问题
深度学习和神经网络的介绍
maxwell 报错(连接为mysql 8.x)解决方法
華大單片機KEIL報錯_WEAK的解决方案
网上期货开户安全么?
华大单片机KEIL报错_WEAK的解决方案
shell脚本常用命令(四)
Error reported by Huada MCU Keil_ Weak's solution