博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive2389 ZOJ1078 Palindrom Numbers【回文+进制】
阅读量:6716 次
发布时间:2019-06-25

本文共 3426 字,大约阅读时间需要 11 分钟。


Time Limit: 2 Seconds       
Memory Limit: 65536 KB

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


Source: 
South America 2001

Regionals 2001 >> Latin America - South America

问题链接:。入门训练题,用C语言编写程序。

题意简述:输入若干个整数,0作为结束。对于输入的整数n,问其几进制为回文数?

问题分析

这是一个有关进制处理的问题,都是套路。

程序中,封装了一个函数ispalindrom()用于将整数转换为指定进制的字符串,同时判断是否为回文数。使用数组ans[]作为标志,其元素个数需要多一个,是否为回文数的标志放在ans[0]中。

程序说明

原来的C语言程序不够简洁,又写了一版C++的程序。

UVALive的问题与ZOJ的问题微妙的差异需要注意,ZOJ中不存在回文时,输出的字符串为:“Number 19 is not a palindrom”,多一个“a”,坑啊!!!

AC的C++语言程序如下:

/* UVALive2389 ZOJ1078 Palindrom Numbers */#include 
#include
using namespace std;const int N = 16;bool ispalindrome(int product, int base){ int miror = 0, temp; temp = product; while(temp) { miror *= base; miror += temp % base; temp /= base; } return miror == product;}int main(){ int n; int ans[N+1]; while(cin >> n && n) { memset(ans, 0, sizeof(ans)); for(int i=2; i<=N; i++) if(ispalindrome(n, i)) ans[0] = 1, ans[i] = 1; if(ans[0]) { cout << "Number " << n << " is palindrom in basis"; for(int i=2; i<=N; i++) if(ans[i]) cout << " " << i; cout << endl; } else cout << "Number " << n << " is not palindrom" << endl; } return 0;}

AC的C语言程序如下:

/* UVALive2389 ZOJ1078 Palindrom Numbers */#include 
#include
#define MAXN1 20000#define MAXN2 16char 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("%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;}

转载于:https://www.cnblogs.com/tigerisland/p/7564497.html

你可能感兴趣的文章
Kinect for Windows SDK开发学习相关资源
查看>>
Android 类中类广播的静态注册方法
查看>>
Requests库上传文件时UnicodeDecodeError: 'ascii' codec can't decode byte错误解析
查看>>
MapReduce中,new Text()引发的写入HDFS的输出文件多一列的问题
查看>>
Windows Phone本地数据库(SQLCE):8、DataContext(翻译)
查看>>
SGU 406 Goggle
查看>>
〖Linux〗Shell十进制数值转换十六进制
查看>>
java设计模式--行为型模式--状态模式
查看>>
mysql学习笔记 第六天
查看>>
MVC4 + EF为Model添加单独的验证属性
查看>>
Oracle用游标删除重复数据
查看>>
数组指针
查看>>
OpenStreetMap初探(一)——了解OpenStreetMap
查看>>
安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
查看>>
js中substr与substring的差别
查看>>
A06_RelativeLayout的属性设置
查看>>
Quartz中时间表达式的设置-----corn表达式
查看>>
javac: cannot execute binary file
查看>>
使用instantclient_11_2 和PL/SQL Developer工具包连接oracle 11g远程数据库
查看>>
使用Ajax的Time实现倒计时功能
查看>>