palindrom C++ 程序能力自主训练课程设计

 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
11
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.

问题陈述
如果一个数字在从左到右或从右到左阅读时是理智的,我们就说它是回文。 例如,数字 75457 是回文。
当然,属性取决于表示数字的基础。 数字 17 不是以 10 为底的回文,但它以 2 为底(10001)的表示是回文。
这个问题的目的是验证一组给定的数字是否是从 2 到 16 的任何基础上的回文。
输入格式
几个整数组成输入。 每个数字 0 < n < 50000 以十进制形式在单独的行中给出。 输入以零结束。
输出格式
11
您的程序必须打印消息 Number i is palindrom in base where I 是给定的数字,然后是数字表示为回文的基础。 如果数字不是 2 到 16 之间的任何基数的回文,您的程序必须打印消息 Number i is not palindrom。

#include<iostream>
using namespace std;
#include<bits/stdc++.h>

#define Maxn1 50000  
#define Maxn2 16  

char t[Maxn1];

int ispalindrom(int val, int base)
{
    int count = 0, start, end;

    while (val) {
        t[count++] = val % base;     
        val /= base;                 
                                    
    }

    start = 0;
    end = count - 1;
    count = 1;
    while (start < end) {
        if (t[start] != t[end]) {
            count = 0;
            break;
        }

        start++;
        end--;
    }

    return count;
}

int main(void)
{
    int n, i;
    int ans[Maxn2 + 1];

    while (scanf_s("%d", &n) != EOF && n != 0) {
        memset(ans, 0, sizeof(ans));

        for (i = 2; i <= Maxn2; i++)
            if (ispalindrom(n, i)) {
                ans[0] = 1;
                ans[i] = 1;
            }

        if (ans[0]) {
            printf("Number %d is palindrom in basis", n);
            for (i = 2; i <= Maxn2; i++)
                if (ans[i])
                    printf(" %d", i);
            printf("\n");
        }
        else
            printf("Number %d is not palindrom\n", n);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值