pat-a1024. Palindromic Number (25
来源:互联网 发布:mac怎么下载歌 编辑:程序博客网 时间:2024/06/16 07:40
水题。。按步骤写就过了。。
#include<string>#include<iostream>#include<algorithm>using namespace std;bool ispro(string t){int len=t.size();int s=len/2;for(int i=0;i<s;++i) if(t[i]!=t[len-1-i]) return 0;return 1;}void fun(string& t){int k=0;string ct=t;string cz=t;reverse(ct.begin(),ct.end());int len=ct.size();for(int i=len-1;i>=0;--i){int sum=ct[i]+cz[i]-96+k;t[i]=sum%10+'0';k=sum/10;}if(k) t='1'+t;}int main(){string t;int step,ok=0;cin>>t>>step;for(int i=0;i<step;++i){if(ispro(t)){ok=1;cout<<t<<endl;cout<<i<<endl;break;}else fun(t);}if(!ok) cout<<t<<endl<<step<<endl;return 0;}
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
- PAT-A1024. Palindromic Number (25)
- pat-a1024. Palindromic Number (25
- PAT A1024. Palindromic Number (25)
- PAT甲级练习题A1024. Palindromic Number (25)
- A1024. Palindromic Number (25)
- 大数运算-PAT A1024.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-A1024
- 【PAT Advanced Level】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)【精华】
- 浙大PAT 1024. Palindromic Number (25)
- PAT (Advanced) 1024. Palindromic Number (25)
- 重拾CSS(1)CSS中的margin属性
- c++ 凌驾于字符串之上的string类
- android加固系列—2.加固前先要学会破解,调试内存值修改程序走向
- oracle分析函数技术详解(配上开窗函数over())
- 百练2801解题报告---填词
- pat-a1024. Palindromic Number (25
- Boolean.valueOf(true)和true的区别
- vue项目,在进行点击的时候,如何屏蔽掉一些小的按钮,阻止冒泡
- 数据结构::浅析平衡二叉树
- 乒乓球-洛谷 1042
- 线性规划与网络流24——魔术球问题
- android加固系列—3.加固前先学会破解,静态修改so
- Android面试题
- javaweb 登陆aes-公钥加密