1078 Palindrom Numbers
2008-02-15 13:46
302 查看
Palindrom NumbersTime limit: 1 Seconds Memory limit: 32768K
Total Submit: 5044 Accepted Submit: 2193 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
Problem Source: South America 2001
http://acm.zju.edu.cn/show_problem.php?pid=1078
#include <iostream>
using namespace std;
int convert(int n, int list[], int basis); //转换进制
int main()
...{
int num[1000];
bool flag1, flag2;
int basis[17], counter = 1;
int n, len;
cin >> n;
while ( n != 0 )
...{
flag1 = 1;
counter = 1;
for ( int i = 2; i < 17; i++ )
...{
len = convert(n, num, i)+1;
flag2 = true;
for ( int j = 0; j < len/2; j++ )
...{
if ( num[j] != num[len-j-1] )
...{
flag2 = false; //flag2标志该数在该进制下是否回文数
break;
}
}
if ( flag2 )
...{
flag1 = 0;
basis[counter] = i;
counter++;
}
}
if ( !flag1 ) //flag1 标志是否有解
...{
cout << "Number " << n << " is palindrom in basis";
for ( int k = 1; k < counter; k++ )
...{
cout << " " << basis[k];
}
cout << endl;
}
else
...{
cout << "Number " << n << " is not a palindrom" << endl;
}
cin >> n;
}
return 0;
}
int convert(int n, int list[], int basis)
...{
int total = 0;
while ( n >= basis )
...{
list[total] = n % basis;
total++;
n /= basis;
}
list[total] = n;
return total;
}
Total Submit: 5044 Accepted Submit: 2193 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
Problem Source: South America 2001
http://acm.zju.edu.cn/show_problem.php?pid=1078
#include <iostream>
using namespace std;
int convert(int n, int list[], int basis); //转换进制
int main()
...{
int num[1000];
bool flag1, flag2;
int basis[17], counter = 1;
int n, len;
cin >> n;
while ( n != 0 )
...{
flag1 = 1;
counter = 1;
for ( int i = 2; i < 17; i++ )
...{
len = convert(n, num, i)+1;
flag2 = true;
for ( int j = 0; j < len/2; j++ )
...{
if ( num[j] != num[len-j-1] )
...{
flag2 = false; //flag2标志该数在该进制下是否回文数
break;
}
}
if ( flag2 )
...{
flag1 = 0;
basis[counter] = i;
counter++;
}
}
if ( !flag1 ) //flag1 标志是否有解
...{
cout << "Number " << n << " is palindrom in basis";
for ( int k = 1; k < counter; k++ )
...{
cout << " " << basis[k];
}
cout << endl;
}
else
...{
cout << "Number " << n << " is not a palindrom" << endl;
}
cin >> n;
}
return 0;
}
int convert(int n, int list[], int basis)
...{
int total = 0;
while ( n >= basis )
...{
list[total] = n % basis;
total++;
n /= basis;
}
list[total] = n;
return total;
}
相关文章推荐
- ZJU1078-Palindrom Numbers
- zoj 1078 Palindrom Numbers
- ZOJ-1078-Palindrom Numbers
- UVALive2389 ZOJ1078 Palindrom Numbers
- ZOJ&nbsp;1078&nbsp;Palindrom&nbsp;Numbers
- ZOJ 1078 Palindrom Numbers
- zoj1078 Palindrom Numbers
- ZOJ1078-Palindrom Numbers
- 判断一个数在2-16进制下是否为回文 Palindrom Numbers
- POJ - 2739 Sum of Consecutive Prime Numbers(尺取法)
- CodeForces 13A - Numbers
- HDOJ 题目1078 FatMouse and Cheese(记忆化搜索+DP)
- [LeetCode]Compare Version Numbers
- CF#310-B - Case of Fake Numbers-模拟水题
- LintCode "Interleaving Positive and Negative Numbers"
- SGU 258 Almost Lucky Numbers 接近幸运数(数位DP)
- Compare Version Numbers
- leetcode之AddTwoNumbers
- HDU 1078 FatMouse and Cheese 简单DP&记忆化搜索
- UESTC 87 Easy Problem With Numbers(扩展欧几里得+线段树)