1024. Palindromic Number (25) 回文字符串
来源:互联网 发布:python 文件读取 编辑:程序博客网 时间:2024/06/13 21:51
1024. Palindromic Number (25)
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 3Sample Output 1:
4842Sample Input 2:
69 3Sample Output 2:
13533
#include<iostream>#include<vector>#include<algorithm>#include<map>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;}
- 1024. Palindromic Number (25) 回文字符串
- 【PAT】1024. Palindromic Number 回文反转相加
- 回文数(palindromic number)
- 回文数(palindromic number)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- Android中Canvas画图
- 华为oj 统计大写字母个数&&求最大连续bit数
- #define和typedef的区别
- zedboard学习记录
- 1455: 罗马游戏|左偏树
- 1024. Palindromic Number (25) 回文字符串
- 分享一套 python 试题
- linux下su和su-以及sudo的介绍
- vs2013编译 x264
- Python学习笔记-列表的条件过滤生成
- 第十五章编程练习(1)
- Quartz2D - 基本图形绘制(线条)
- android开发中是用Intent从一个activity跳向一个activity的问题
- ORA-01034 与ORA-01034