博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1024 Palindromic Number
阅读量:4541 次
发布时间:2019-06-08

本文共 2200 字,大约阅读时间需要 7 分钟。

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:

4842

Sample Input 2:

69 3

Sample Output 2:

13533
1 #include 
2 #include
3 using namespace std; 4 int main() 5 { 6 string N, str; 7 int K, i = 0; 8 cin >> N >> K; 9 if (N == str.assign(N.rbegin(), N.rend()))10 K = 0;11 for (i = 1; i <= K; ++i)12 {13 str = "";14 int s = 0;15 for (int j = 0; j < N.length(); ++j)16 {17 str += (s + N[j] - '0' + N[N.length() - j - 1] - '0') % 10 + '0';18 s = (s + N[j] - '0' + N[N.length() - j - 1] - '0') / 10; 19 }20 if (s > 0)21 str += s + '0';22 N.assign(str.rbegin(), str.rend());23 if (N == str)24 break;25 }26 cout << N << endl;27 cout << (i > K ? K : i) << endl;28 return 0;29 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11215971.html

你可能感兴趣的文章
7bit ASCII编解码
查看>>
flask-sqlalchemy(包含离线脚本,with在上下文管理的应用)
查看>>
机器学习工程师 - Udacity 强化学习 Part Ten
查看>>
go语言 新手学习笔记 go基础教程
查看>>
zabbix 添加宏变量
查看>>
2016年11月1日——jQuery源码学习笔记
查看>>
Thinkphp5笔记二:创建模块
查看>>
centos 安装mysql
查看>>
Redis 禁用FLUSHALL FLUSHDB KEYS 命令
查看>>
Matlab中imread函数使用报错“不应为MATLAB 表达式”分析
查看>>
MFC ADO数据库操作
查看>>
图像质量评价-NQM和WPSNR
查看>>
面试准备——相关知识
查看>>
每日一字:悟
查看>>
CentOS7.6安装稳定版Nginx
查看>>
LeetCode 1002. Find Common Characters (查找常用字符)
查看>>
建立隐藏管理员用户
查看>>
android设置图文提醒功能
查看>>
ajax跨域提交
查看>>
完成登录与注册页面的前端
查看>>