中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
ACM/ICPC Programming Exercise -- 1078 Palindrom Numbers
作者:未知 时间:2005-07-27 23:31 出处:CSDN 责编:chinaitpower
              摘要:ACM/ICPC Programming Exercise -- 1078 Palindrom Numbers
URL: http://acm.zju.edu.cn/show_problem.php?pid=1078

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

My Solution: (C++, GCC)

I am wondering if another way does exit in which I can get the final answer beside mine given below. May be you have an more effective solution. If it is true, simply send it to my email address ichobits@21cn.com, I would be very pleased to having a discussion with you. 

#include <iostream>
#include <vector>
// PS: The code is for study purpose only, never submit it as ones own.
// From: http://blog.csdn.net/mskia/
// email: ichobits@21cn.com
int main(void) {
    int num;
    while (std::cin >> num && num != 0) {
        std::vector<int> result;
        for (int i = 2; i <= 16; ++i) {
            std::vector<int> bn;
            int tmp = num;
            
            while (tmp > 0) {
                bn.push_back(tmp % i);
                tmp /= i;
            }
            
            std::vector<int>::const_iterator left;
            std::vector<int>::const_iterator right = bn.end();
            for (left = bn.begin(), --right; left < right; ++left, --right) {
                if (*left != *right) {
                    break;
                }
            }
            
            if (left >= right) {
                result.push_back(i);
            }    
        }
        
        std::cout << "Number " << num << " is ";
        if (result.size() == 0) {
            std::cout << "not a palindrom" << std::endl;
        } else {
            std::cout << "palindrom in basis";
            for (std::vector<int>::const_iterator p = result.begin()
            ; p != result.end(); ++p) {
                std::cout << ' ' << *p;
            }
            std::cout << std::endl;
        }    
    }    
    
    return 0;
}    


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有