zoukankan      html  css  js  c++  java
  • 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 $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);
    
    			printf("%d",i);
    			break;
    		}
    		else
    		{
    			transfer(n,ans);
    			add(n,ans);
    		}	
    	}
    	if(i==k)
    	{
    		printf("%s
    ",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]='';
    
    
    }
    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,'',202);
    	j=0;
    	for(i=len-1;i>=0;i--)
    	{
    		n[j++]=tmp[i];
    	}
    }
    
    
  • 相关阅读:
    洛谷 P1040 加分二叉树
    洛谷 P1892 团伙
    洛谷 P2024 食物链
    洛谷 P1196 银河英雄传说
    并查集--算法,优化,变种
    洛谷 P1801 黑匣子_NOI导刊2010提高(06)
    洛谷 P3370 【模板】字符串哈希
    洛谷 P1090 合并果子
    洛谷 P1219 八皇后
    线的缩放效果
  • 原文地址:https://www.cnblogs.com/zhengkang/p/5735971.html
Copyright © 2011-2022 走看看