博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1024. Palindromic Number (25) 回文字符串
阅读量:4072 次
发布时间:2019-05-25

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

1024. Palindromic Number (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
AC代码
#include
#include
#include
#include
using namespace std;string BigNumAdd(string a,string b){ string res = ""; int c = 0; for (int i = (int)a.size()-1;i>=0; --i) { c = a[i]-'0'+b[i]-'0'+c; char ch = c%10 +'0'; res = ch+res; c = c/10; if(i==0&&c!=0){ ch = c+'0'; res = ch+res; } } return res;}int main(){ int count = 0,step; string num,fanNum; cin>>num>>step; while (true) { fanNum = num; reverse(fanNum.begin(), fanNum.end()); if(num==fanNum) break; num = BigNumAdd(num, fanNum); count++; if(count==step) break; } printf("%s\n%d\n",num.c_str(),count); return 0;}

转载地址:http://mmhji.baihongyu.com/

你可能感兴趣的文章
最完整的Java IO流学习总结
查看>>
Android开发中Button按钮绑定监听器的方式完全解析
查看>>
解决ScrollView嵌套ListView后,进入页面不从顶部开始
查看>>
基于Rxjava2的事件总线:Rxbus
查看>>
Android6.0动态权限获取框架:RxPermission(基于RxJava2)
查看>>
Android中解决华为手机设置PopupWindow半透明背景无效果问题
查看>>
解决三星note3调用系统拍照后程序崩溃或无法获取图片
查看>>
序列化Serializable和Parcelable的区别
查看>>
Android自定义View绘制真正的居中文本
查看>>
Android贝塞尔曲线实现加入购物车抛物线动画
查看>>
Android自定义View实现商品评价星星评分控件
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
postgresql查看表的和索引的情况,判断是否膨胀
查看>>
postgresql中根据oid和filenode去找表的物理文件的位置
查看>>
postgresql中wal日志什么时候会触发归档
查看>>
Centos 6.8 上 DRBD安装和使用
查看>>
history查看历史操作记录,并显示操作时间
查看>>
postgresql修改完端口后直接psql连接数据库报错
查看>>
pl/proxy-2.5安装在postgresql9.6上无法编译
查看>>
postgresql遇到“Connection refused”和“No route to host”大概的解决方法
查看>>