1024. Palindromic Number (25)
2016-02-11 15:00
363 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
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:
484 2
Sample Input 2:
69 3
Sample Output 2:
1353 3
#include <iostream> #include <vector> #include <deque> #include <string> #include <algorithm> using namespace std; string reverse(string old) { reverse(old.begin(), old.end()); return old; } inline char int2char(int x) { return x + '0'; } inline int char2int(char c) { return c - '0'; } string sum(string a, string b) { int c = 0; string sum; for (int i = a.size()-1; i >=0; i--) { int s = char2int(a[i]) + char2int(b[i])+c; sum.insert(sum.begin(), int2char(s%10)); c = s / 10; } if (c != 0) { sum.insert(sum.begin(), int2char(c)); } return sum; } int main() { string number; string rev; int k; cin >> number >> k; int c = 0; for (c = 0; c <= k;c++) { rev = reverse(number); if (number == rev || c==k) { break; } else { number = sum(number, rev); } } cout << number << endl << c << endl; system("pause"); return 0; }
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)