1024. Palindromic Number (25)
来源:互联网 发布:python snmpgetbulk 编辑:程序博客网 时间:2024/05/20 11:19
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 3
Sample Output 1:
4842
Sample Input 2:
69 3
Sample Output 2:
13533
#include<iostream>#include<string>using namespace std;string exchange(string a){int i;string b;for(i=0;i<a.size();i++)b=a[i]+b;return b;}string add(string a,string b){int i=a.size();char r;int t1=0,t2=0,t3=0;string c;while(--i>=0){t3=0;t1=a[i]-'0'+b[i]-'0';if(t1%10+t2<10)r=(t1%10+t2) +'0';else{r='0';t3=1;}c=r+c;t2=t1/10+t3;}if(t2==1)c='1'+c;return c;}int main(){string N,M;int K,i=0;cin>>N>>K;while(i<K){M=exchange(N);if(M==N)break;N=add(N,M);i++;}cout<<N<<endl<<i<<endl;}感想:数字最大大约可以到(2^100 )* 10^10,所以不能用整型,一定要用字符串储存,模拟手算得到结果ps:这道题用python会不会更简单
- 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)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- 1024. Palindromic Number (25)
- Codeforces Round #394 (Div. 2)(A. Dasha and Stairs,B. Dasha and friends,C. Dasha and Password)
- 数据来源
- 第一篇博,写点什么
- 脱壳-PC Guard(4.06)
- mysql查漏补缺
- 1024. Palindromic Number (25)
- Test
- BZOJ 4551([Tjoi2016&Heoi2016]树-倒序并查集)
- Leetcode 67. Add Binary
- Leetcode
- 34. Search for a Range
- Leetcode 13. Roman to Integer
- <Python初学>基本数据str的操作
- 10004UVa二染色