ZOJ1078: palindrom number(回文数)

os posted @ 2013年9月09日 13:39 in ACM , 777 阅读

acm 题目List

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1078

 

#include <stdio.h>

int main()
{
	int n;
	int i,j;

	while(scanf("%d", &n) && n)
	{
		int sign = 0;
		char c[30] = {0};
		int base[17] = {0};

		for (i = 2; i <= 16; i++)
		{
			int m = n;
			int len = 0;
			while (m)
			{
				c[len++] = m%i;
				m = m/i;
			}

			sign = 1;
			for (j = 0; j < len/2&&sign; j++)
				if (c[j] != c[len -j -1]) sign = 0;
			if (sign) base[i] = 1;
		}

		sign = 0;
		for (i = 2; i <= 16; i++)
			if (base[i] == 1) sign = 1;

		if (!sign)
			printf("Number %d is not a palindrom\n", n);
		else
		{
			printf("Number %d is palindrom in basis", n);
			for (i = 2; i <= 16; i++)
				if (base[i] == 1) 
					printf(" %d", i);
			printf("\n");
		}
	}

	return 0;
}

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter