PAT-A 1024. Palindromic Number
2016-08-04 11:19
423 查看
1024. Palindromic Number
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 (<= 10^{10}) $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<stdio.h> #include<string.h> #include<stdlib.h> char n[202]={0}; int isPalindromic(char ans[]); void transfer(char n[],char ans[]); void transfer(char n[],char ans[]); void add(char n[],char ans[]); char ans[202]; int main() { int k; scanf("%s %d",n,&k); int i; for(i=0;i<k;i++) { if(isPalindromic(n)) { printf("%s\n",n); printf("%d",i); break; } else { transfer(n,ans); add(n,ans); } } if(i==k) { printf("%s\n",n); printf("%d",k); } return 0; } void transfer(char n[],char ans[]) { int i,len = strlen(n),j=0; for(i=len-1;i>=0;i--) { ans[j++]=n[i]; } ans[j]='\0'; } int isPalindromic(char ans[]) { int len= strlen(ans),j; int i=0; j=len -1; while(i<j) { if(ans[i]!=ans[j]) return 0; i++; j--; } return 1; } void add(char n[],char ans[]) { char tmp[200]={0}; int len = strlen(n); int i=len-1,j=0; int flag=0; int sum = 0; for(i=len-1;i>=0;i--) { sum = (n[i]-'0'+ans[i]-'0')+flag; tmp[j++]=sum%10+'0'; flag = sum/10; } if(flag) tmp[j]=flag +'0'; len = strlen(tmp); memset(n,'\0',202); j=0; for(i=len-1;i>=0;i--) { n[j++]=tmp[i]; } }
相关文章推荐
- left join 和 left outer join 的区别
- atags_to_fdt.c
- oracle中单引号和双引号的作用
- Makefile 使用总结
- Git 使用规范流程
- redis主从集群搭建及容灾部署(哨兵sentinel)
- java同步数据时候处理生僻字
- KVM学习笔记(二)--虚拟机克隆
- iOS开发——消息推送跳转
- Codeforces Round #286 (Div. 2) C dp
- Cookie/Session机制详解与区别
- 【javase复习】## day9 Collections Arrays ##
- ExtJs:获取一个grid上面的所有内容
- python单元测试unittest
- java小技巧
- 小师妹问 easyUI mergeCells 行合并后表头和内容对不齐
- jdbc
- 手机的分辨率
- html+js+highcharts绘制圆饼图表的简单实例
- ubuntu sever 的一些常用操作