PAT甲级——A1024 Palindromic Number
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 (≤) is the initial numer and K (≤) 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 <string>
using namespace std;
int main()
{
string N, str;
int K, i = ;
cin >> N >> K;
if (N == str.assign(N.rbegin(), N.rend()))
K = ;
for (i = ; i <= K; ++i)
{
str = "";
int s = ;
for (int j = ; j < N.length(); ++j)
{
str += (s + N[j] - '' + N[N.length() - j - ] - '') % + '';
s = (s + N[j] - '' + N[N.length() - j - ] - '') / ;
}
if (s > )
str += s + '';
N.assign(str.rbegin(), str.rend());
if (N == str)
break;
}
cout << N << endl;
cout << (i > K ? K : i) << endl;
return ;
}
PAT甲级——A1024 Palindromic Number的更多相关文章
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- PAT A1024 Palindromic Number (25 分)——回文,大整数
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT A1019 General Palindromic Number (20 分)——回文,进制转换
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- A1024. Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT 1019 General Palindromic Number
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- 【PAT】1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- [PAT] A1019 General Palindromic Number
[题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...
随机推荐
- jeecms jeecmsv93建库
create tablespace jeecms93 datafile 'jeecms93.dbf' size 100M reuse autoextend on next 50M;1. 2.drop ...
- 豌豆荚Redis集群方案:Codis
Codis简介 Codis是一个分布式Redis解决方案,对于上层的应用来说,连接到CodisProxy和连接原生的RedisServer没有明显的区别(不支持的命令列表),上层应用可以像使用单机的R ...
- 19.SimLogin_case04
# 利用cookies登录马蜂窝 import requests from lxml import etree session = requests.Session() phone_number = ...
- Java学习之垃圾回收机制
垃圾回收机制,依赖JRE和JVM,涉及操作系统中内存的分配与回收.依据所学,我猜想这种机制需要的数据结构是堆内存分配表(链),管理已分配和未分配的堆内存,对于已分配堆内存,需要知道由栈内存中的哪些变量 ...
- 2019 CCPC 湖南全国邀请赛
A. Chessboard 做法1 单纯形. 做法2 最大费用可行流问题,行列模型. 对每行建一个点,每列建一个点.物品 \(i\) 在 \((r,c)\),那么 \(r\) 向 \(c\) 连流量为 ...
- C# 字符串的日期比较
SearchResult = SearchResult.Where(v => (DateTime.Parse(v.CreateTime.ToString("yyyy/MM/dd&quo ...
- Activit单元i测试(与spring集成测试)
1.测试 eclipse下安装activiti插件以及maven 右键新建activiti project(这时会自动创建pom依赖以及activiti.cfg.xml,但还不是maven项目) 选中 ...
- JS规则 确定你的存在(变量声明) 声明变量语法: var 变量名; 一次声明多个,中间用逗号隔开var num1,mun2 ;
确定你的存在(变量声明) 我们要使用盒子装东西,是不是先要找到盒子,那在编程中,这个过程叫声明变量,找盒子的动作,如何表示: 声明变量语法: var 变量名; var就相当于找盒子的动作,在JavaS ...
- scoreboarding
Reference docs: https://en.wikipedia.org/wiki/Scoreboarding SSC_course_5_Scoreboard_ex.pdf 1, what i ...
- iOS之UITableView加载网络图片cell自适应高度
#pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...