Reverse and Add

Reverse and Add

时间: 1ms        内存:64M

描述:

The reverse and add function starts with a number, reverses its digits and adds the reverse to the original. If the sum is not a palindrome (meaning it does not give the same number read from left to right and right to left), we repeat this procedure until it does.

For example, if we start with 195 as the initial number, we get 9,339 as the resulting palindrome after the fourth addition:

195 786 1,473 5,214
591 687 3,741 4,125
+ --- + --- + --- + ---
786 1,473 5,214 9,339

This method leads to palindromes in a few steps for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It has never been proven, however, that no such palindrome exists.

You must write a program that takes a given number and gives the resulting palindrome (if one exists) and the number of iterations/additions it took to find it.

You may assume that all the numbers used as test data will terminate in an answer with less than 1,000 iterations (additions), and yield a palindrome that is not greater than 4,294,967,295.

输入:

The first line will contain an integer N ( 0 < N100), giving the number of test cases, while the next N lines each contain a single integer P whose palindrome you are to compute.

输出:

For each of the N integers, print a line giving the minimum number of iterations to find the palindrome, a single space, and then the resulting palindrome itself.

示例输入:

3
195
265
750

示例输出:

4 9339
5 45254
3 6666

提示:

参考答案(内存最优[752]):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int hui( char b[] );
void add(char a[],char b[],char back[]);

int main()
{
	int i, j;
	char a[100], b[100],c[100];
	int n, len;
	int cnt;
	
	
	scanf("%d", &n);
	getchar();
	
	for( i = 0; i < n; i++ )
	{
		cnt = 1;
		memset(a, 0, sizeof(a));
		memset(c, 0, sizeof(c));
		memset(b,0, sizeof(b));
		gets(a);
		len = strlen(a);
		for( j = len-1; j >= 0; j-- )
		{
			c[j] = a[len-1-j];
		}
		while(1)
		{
			add(a, c, b);
			if( hui(b) == 0 )
			{
				break;
			}
			memset(a, 0, sizeof(a));
			memset(c, 0, sizeof(c));
			strcpy(a, b);
			
			len = strlen(b); 
			for( j = len-1; j >= 0; j-- )
			{
				c[j] = a[len-1-j];
			}
			cnt++;
			memset(b,0, sizeof(b));
		}
		
		printf("%d ", cnt);
		puts(b);

	}
	
	
	return 0;
}

void add(char a[],char b[],char back[])
{
	int i,j,k,up,x,y,z,l;
	char *c;
	if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
	c=(char *) malloc(l*sizeof(char));
	i=strlen(a)-1;
	j=strlen(b)-1;
	k=0;up=0;
	while(i>=0||j>=0)
	{
		if(i<0) x='0'; else x=a[i];
		if(j<0) y='0'; else y=b[j];
		z=x-'0'+y-'0';
		if(up) z+=1;
		if(z>9) {up=1;z%=10;} else up=0;
		c[k++]=z+'0';
		i--;j--;
	}
	if(up) c[k++]='1';
	i=0;
	c[k]='\0';
	for(k-=1;k>=0;k--)
		back[i++]=c[k];
	back[i]='\0';
} 

int hui( char a[] )
{
	int i;
	int flag = 0;
	int len;
	
	len = strlen(a);
	
	for( i = 0; i < len/2; i++ )
	{
		if( a[i] != a[len-1-i] )
		{
			flag = 1;
		}
	}
	if( flag )
	{
		return 1;
	}
	return 0;
	
}

参考答案(时间最优[0]):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int hui( char b[] );
void add(char a[],char b[],char back[]);

int main()
{
	int i, j;
	char a[100], b[100],c[100];
	int n, len;
	int cnt;
	
	
	scanf("%d", &n);
	getchar();
	
	for( i = 0; i < n; i++ )
	{
		cnt = 1;
		memset(a, 0, sizeof(a));
		memset(c, 0, sizeof(c));
		memset(b,0, sizeof(b));
		gets(a);
		len = strlen(a);
		for( j = len-1; j >= 0; j-- )
		{
			c[j] = a[len-1-j];
		}
		while(1)
		{
			add(a, c, b);
			if( hui(b) == 0 )
			{
				break;
			}
			memset(a, 0, sizeof(a));
			memset(c, 0, sizeof(c));
			strcpy(a, b);
			
			len = strlen(b); 
			for( j = len-1; j >= 0; j-- )
			{
				c[j] = a[len-1-j];
			}
			cnt++;
			memset(b,0, sizeof(b));
		}
		
		printf("%d ", cnt);
		puts(b);

	}
	
	
	return 0;
}

void add(char a[],char b[],char back[])
{
	int i,j,k,up,x,y,z,l;
	char *c;
	if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
	c=(char *) malloc(l*sizeof(char));
	i=strlen(a)-1;
	j=strlen(b)-1;
	k=0;up=0;
	while(i>=0||j>=0)
	{
		if(i<0) x='0'; else x=a[i];
		if(j<0) y='0'; else y=b[j];
		z=x-'0'+y-'0';
		if(up) z+=1;
		if(z>9) {up=1;z%=10;} else up=0;
		c[k++]=z+'0';
		i--;j--;
	}
	if(up) c[k++]='1';
	i=0;
	c[k]='\0';
	for(k-=1;k>=0;k--)
		back[i++]=c[k];
	back[i]='\0';
} 

int hui( char a[] )
{
	int i;
	int flag = 0;
	int len;
	
	len = strlen(a);
	
	for( i = 0; i < len/2; i++ )
	{
		if( a[i] != a[len-1-i] )
		{
			flag = 1;
		}
	}
	if( flag )
	{
		return 1;
	}
	return 0;
	
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注