当前位置:网站首页>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;
}
边栏推荐
- 基于STM32F103ZET6库函数跑马灯实验
- What is ssr/ssg/isr? How do I host them on AWS?
- One to one relationship
- 教你打印自己的日志 -- 如何自定义 log4j2 各组件
- 云笔记到底哪家强 -- 教你搭建自己的网盘服务器
- Error reported by Huada MCU Keil_ Weak's solution
- 信息学奥赛一本通 1335:【例2-4】连通块
- 现在网上买股票开户身份证信息安全吗?
- Function key input experiment based on stm32f103zet6 Library
- (LC)46. 全排列
猜你喜欢

Crawl national laws and Regulations Database

DFS and BFS simple principle

crontab的学习随笔

Erreur Keil de Huada Single Chip Computer La solution de Weak

C# 二维码生成、识别,去除白边、任意颜色

Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders

Introduction to deep learning and neural networks

Running lantern experiment based on stm32f103zet6 library function

拥抱云原生:江苏移动订单中心实践

数仓的字符截取三胞胎:substrb、substr、substring
随机推荐
图扑数字孪生智慧能源一体化管控平台
破解仓储难题?WMS仓储管理系统解决方案
Oracle 获取月初、月末时间,获取上一月月初、月末时间
Where to look at high-yield bank financial products?
Tupu digital twin intelligent energy integrated management and control platform
Keras deep learning practice (12) -- facial feature point detection
【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
在线文本按行批量反转工具
工作流自动化 低代码是关键
New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
shell脚本常用命令(三)
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
信息学奥赛一本通 1335:【例2-4】连通块
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
芯动联科冲刺科创板:年营收1.7亿 北方电子院与中城创投是股东
Current market situation and development prospect forecast of the global ductless heating, ventilation and air conditioning system industry in 2022
数仓的字符截取三胞胎:substrb、substr、substring
基于STM32F103ZET6库函数按键输入实验