laizhuoyu 2017-03-19 16:09 采纳率: 0%
浏览 1144
已结题

ZOJ 1078 Palindrom Numbers

Statement of the Problem
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

#include <iostream>

using namespace std;
void change_base_palindrom(int num)
{
    int i,j,number[100]={0},base,judge,base_num[15]={0},older=0,n=num;
    for(base=2;base<17;base++)
    {
        num=n;
        for(i=0;num!=0;i++)
        {
            number[i]=num%base;
            num/=base;
        }
        for(judge=0,j=0;j<i/2;j++)
        {
            if(number[j]!=number[i-j-1])
                judge+=1;
        }
        if(judge==0)
        {
            base_num[older]=base;
            older+=1;
        }
    }
    if(older==0)
        cout<<"Number "<<n<<" is not a palindrom"<<endl;
    else
        {
            cout<<"Number "<<n<<" is palindrom in basis";
            for(i=0;i<older;i++)
                cout<<' '<<base_num[i];
            cout<<endl;
        }



}
int main()
{
    int n;
    cin>>n;
    while(n)
    {
        if(n>0&&n<50000)
            change_base_palindrom(n);
        cin>>n;
    }
    return 0;
}

为什么不能通过

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境