zoj 1078 palindrom numbers

 题目见zoj 1078

 主要是判断一个整数在基数为2-16之间的某个数字时是否为回文,我是直接该整数转换成对应基数的表示的逆序列,并计算出该表示下的值,判断是否等于这个整数值,如果相等,那么就是回文,如果不相等就不是。

 

/* zoj 1078 Palindrom Numbers */
#include <stdio.h>

#define MAX 30

int isPalindrom(int base, int decNum);

int main(void)
{
  int i,count;
  int n,baseStr[MAX];
  
  while(scanf("%d", &n) == 1 && n != 0)
    {
      count = 0;
      for(i = 2; i <= 16; i++)
	if(isPalindrom(i, n))
	  baseStr[count++] = i;
      if(count == 0)
	printf("Number %d is not a palindrom\n", n);
      else
	{
	  printf("Number %d is palindrom in basis",n);
	  for(i = 0; i < count; i++)
	    printf(" %d",baseStr[i]);
	  printf("\n");
	}
    }
  return 0;
}
int isPalindrom(int base, int decNum)
{
  int revBaseStr[MAX];
  int i,count;
  int temp = decNum;
  
  for(count = 0; temp > 0; temp /= base)
    revBaseStr[count++] = temp%base;
  for(temp = 0, i = 0; i < count; i++)
    temp = temp * base + revBaseStr[i];
  if(temp == decNum)
    return 1;
  else
    return 0;
}
  
 

 

你可能感兴趣的:(c,ACM,ZOJ)