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];
    }
}
全部评论

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务