1019 General Palindromic Number(进制转化,回文数)
1019 General Palindromic Number
0、题目
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N > 0 N>0 N>0 in base b ≥ 2 b≥2 b≥2, where it is written in standard notation with k + 1 k+1 k+1 digits a i a_i ai as ∑ i = 0 k ( a i b i ) ∑_{i=0}^k(a_ib_i) ∑i=0k(aibi). Here, as usual, 0 ≤ a i < b 0≤ai<b 0≤ai<b for all i i i and a k a_k ak is non-zero. Then N N N is palindromic if and only if a i = a k − i a_i=a_{k−i} ai=ak−i for all i i i. Zero is written 0 in any base and is also palindromic by definition.
Given any positive decimal integer N N N and a base b b b, you are supposed to tell if N N N is a palindromic number in base b b b.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N
and b
, where 0 < N ≤ 1 0 9 0<N≤10^9 0<N≤109 is the decimal number and 2 ≤ b ≤ 1 0 9 2≤b≤10^9 2≤b≤109 is the base. The numbers are separated by a space.
Output Specification:
For each test case, first print in one line Yes
if N
is a palindromic number in base b, or No
if not. Then in the next line, print N as the number in base b
in the form “ a k a k − 1 . . . a 0 a_k a_{k−1} … a_0 akak−1…a0”. Notice that there must be no extra space at the end of output.
Sample Input 1:
27 2
Sample Output 1:
Yes
1 1 0 1 1
Sample Input 2:
121 5
Sample Output 2:
No
4 4 1
1、大致题意
给出一个 10进制数
和 另一个基数b
,问这个 10进制
数转化为 基数b
的数值是不是回文数
2、基本思路
简单题
3、AC代码
#include<iostream>
#include<vector>
using namespace std;
int N,b;
vector<int> a;
void init(int N) {
int n=N,k;
while(n!=0) {
k=n%b;
n/=b;
a.push_back(k);
}
}
int is_Palindromic() {
int size=a.size();
for(int i=0; i<size/2; i++) {
if(a[i]!=a[size-i-1]) {
return -1;
}
}
return 1;
}
void print() {
int size=a.size();
cout<<a[size-1];
for(int i=size-2; i>=0; i--) {
cout<<" "<<a[i];
}
}
int main() {
cin>>N>>b;
init(N);
if(is_Palindromic()==1) {
cout<<"Yes"<<endl;
print();
} else {
cout<<"No"<<endl;
print();
}
return 0;
}
还没有评论,来说两句吧...