博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT A1024 Palindromic Number (25 分)——回文,大整数
阅读量:4322 次
发布时间:2019-06-06

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

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:

4842

Sample Input 2:

69 3

Sample Output 2:

13533
 
#include 
#include
#include
#include
using namespace std;long long tonum(string s) { long long res = 0; for (int i = 0; i < s.length(); i++) { res = res * 10 + s[i] - '0'; } return res;}string tos(long long num) { string res = ""; do { res += '0' + num % 10; num /= 10; } while (num != 0); reverse(res.begin(), res.end()); return res;}string add(string s1, string s2) { reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); string s3=""; int carry = 0; int i = 0; for (i; i < s1.length() && i < s2.length(); i++) { int tmp1 = s1[i] - '0'; int tmp2 = s2[i] - '0'; int tmp = tmp1 + tmp2 + carry; s3 += tmp % 10 + '0'; carry = tmp / 10; } if (s1.length() > s2.length()) { while(i
> s >> k; int j = 0; string s_2; s_2 = s; if (!pali(s)) { for (j = 1; j <= k; j++) { s_2 = s; reverse(s_2.begin(), s_2.end()); s = add(s, s_2); if (pali(s))break; } } if (j == k + 1)j--; cout << s << endl << j; system("pause");}

注意点:又是回文判断题,注意数字可能会超出long long 范围,所以只能用大数相加即两个字符串相加来做,回文的判断直接用了字符串的反转和==。

ps:发现字符串相加想太多了,一个数和他的反转两个数字一定位数是相等的,不用这么麻烦。而且做大整数加法正确的方式是给短的那个数补0,这样直接最后判断一下进位就好了。

转载于:https://www.cnblogs.com/tccbj/p/10395413.html

你可能感兴趣的文章
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>