PAT-A 1024. Palindromic Number

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 <nobr> N(<=1010) </nobr>is the initial numer and <nobr> K(<=100) </nobr> 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];
    }
}
全部评论

相关推荐

面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
AFBUFYGRFHJLP:直接去美帝试试看全奖phd吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务