浙大PAT 1024. Palindromic Number (25)
来源:互联网 发布:做漫画用什么软件 编辑:程序博客网 时间:2024/05/19 22:46
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 <cstdio>#include <cstdlib>#include <cstring>const int maxx = 120;void add(char *sa,char *sb,int len){//big addint tm,c = 0,pos = maxx-1,i;char res[maxx]={0};for(i=len-1;i>=0;--i){tm = (sa[i]-'0')+(sb[i]-'0')+c;c = tm/10;tm %= 10;res[--pos]=tm+'0';}if(c!=0)res[--pos] = c + '0';strcpy(sa,res+pos);}int yes(char *s){int len = strlen(s),i,ret;char st[maxx]={0};for(i=0;i<len;++i){//revsersest[i] = s[len-1-i];}if(strcmp(s,st)==0){//trueret = 1;}else{//false addadd(s,st,len);ret = 0;}return ret;}int main(){int num,k,i,len;char sn[maxx];scanf("%s %d",sn,&k);for(i=0;i<k;++i){if(yes(sn)==1)break;}printf("%s\n%d\n",sn,i);return 0;}
- 浙大PAT 1024. Palindromic Number (25)
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- 浙大PAT 1024题 1024. Palindromic Number
- 浙大 PAT 1019. General Palindromic Number (20)
- 【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
- 【PAT Advanced Level】1024. Palindromic Number (25)
- PAT 1024. Palindromic Number (25)【精华】
- PAT (Advanced) 1024. Palindromic Number (25)
- java基础学习记录之利用数组进行进制转换的学习与练习五
- Mac 操作
- Matlab中的匿名函数的使用
- HDU 1114 又见完全背包问题(必须装满)
- oracle 查看表空间大小
- 浙大PAT 1024. Palindromic Number (25)
- 发现一个c++中代替goto的方法
- 频率抽取(DIF)基2FFT算法的MATLAB实现
- 深入理解OAuth2.0协议
- linux下brctl配置网桥
- poj2262
- JEP 192 String Deduplication in G1
- sql 通配符
- java基础学习记录之利用数组查表法进行进制转换的学习与练习六