We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number 75457 is a palindrom.
Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a palindrom.
The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.
Input Format
Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input ends with a zero.
Output Format
Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print the message Number i is not palindrom.
Sample Input
17
19
0
Sample Output
Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom
以上的是题目,要求判断一个数他的从2到16进制是否是头尾相等的数?
我的程序:
#include <stdio.h>
int fac(int x,int y)
{
int i,a[50],b[50],count=0;
for(i=0,a[0]=x;a[i]==0;i++)
{
a[i+1]=a[i]/y;
b[i]=a[i]%y;
++count;
}
if(count%2==0)
{
for(i=0;i<(int)(1+count)/2-1;i++,count--)
{
if(b[i]=b[count]) return 1;
}
}
if(count%2!=0)
{
for(i=0;i<(int)(1+count)/2;i++,count--)
{
if(b[i]=b[count]) return 1;
}
}
}
main()
{
int n,i,flag=1,flag2=1;
while(flag==1)
{
scanf("%d",&n);
if(n!=0)
{
printf("number %d is ",n);
for(i=2;i<=16;i++)
{
if(fac(n,i)) flag2=0;
else flag2=1;
}
if(flag2==1)
{
printf("palindrom in basis ");
{
for(i=2;i<=16;i++) {printf("%d ",i);}
}
}
if(flag2==0) printf("not a palindrom\n");
}else flag=0;
}
}
运行达不到效果,我知道我的程序写的很是不好,只是初步的~~
请教各位~~了
我运行总是输出“number X is not a palindrom",X 为不输入的数字,为什么呢?