PAT 1024. Palindromic Number (25)(字符串对称,相加)
来源:互联网 发布:英语造句软件下载 编辑:程序博客网 时间:2024/05/18 12:32
官网
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:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3
解题思路
- 1.判断一个数是否对称,如果不对称,问你在给定一个反转的次数内,它本身与翻转数的和是否是对称的。
- 2.全部用字符串相加,字符串翻转,判断字符串是否对称即可。
AC代码
#include<iostream>#include<string>using namespace std;//判断是否对称bool isPal(string a){ for(int i = 0,j = a.length()-1;i<=j;i++,j--){ if (a[i] != a[j]) { return false; } } return true;}//翻转再相加string reversed_add(string a){ string b; b = ""; char c; int next = 0; for (int i = 0, j = a.length() - 1; i < a.length(); i++,j--) { int tem = (a[i] - '0') + (a[j] - '0') + next; if (tem > 9) { tem = tem -10; next = 1; }else { next = 0; } c = tem + '0'; b = c + b; } //最高位相加和大于10的情况 if (next > 0) { c = next + '0'; b = c + b; } return b;}int main(int argc, char *argv[]){ //cout << isPal("12") << endl; //cout << reversed_add("555") << endl; string a; int n; cin >> a >> n; int i = 0; while (!isPal(a)) { a = reversed_add(a); i++; if (i >= n) { break; } } cout << a << endl; cout << i << endl; return 0;}
- PAT 1024. Palindromic Number (25)(字符串对称,相加)
- PAT (Advanced Level) 1024. Palindromic Number (25) 字符串反转相加
- 【PAT 】1024.Palindromic Number (25) 字符操作+大数相加
- 【PAT】1024. Palindromic Number 回文反转相加
- 1024. Palindromic Number (25)-PAT甲级真题(大整数相加)
- 1024. Palindromic Number (25)(PAT甲)
- PAT 1024 Palindromic Number (大整数相加)
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- 【PAT】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- pat 1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)
- PAT 甲级 1024. Palindromic Number(大整数相加的回文数字)
- PAT 1024. Palindromic Number
- PAT 1024. Palindromic Number
- PAT 1024. Palindromic Number
- ReactJS简单介绍
- jquery中ajax同步、异步区别
- 点和向量的转换 Transforming Points and Vectors
- HTML:transition属性学习
- Windows7下,完全、彻底卸载Oracle
- PAT 1024. Palindromic Number (25)(字符串对称,相加)
- linux中PCI总线驱动
- 21个优质Swift开源App
- java 反射解析,将反射用于工厂模式【实例】
- 团队的建设与管理-献给每一位奋斗的同事
- 统计字符数
- JAVA SortedSet的坑
- 大数据分析之聚类算法
- Lightoj1216——Juice in the Glass (计算几何)