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 in base b≥2, where it is written in standard notation with k+1 digits ai as ∑i=0k(aibi). Here, as usual, 0≤ai Given any positive decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b. Input Specification:Each input file contains one test case. Each case consists of two positive numbers N and b, where 0 For each test case, first print in one line |
27 2
- Yes
- 1 1 0 1 1
121 5
- No
- 4 4 1
题目大意
给你一个正整数,将其转换为op进制,并判断其转换为op进制后,是否是回文数
思路
倒序取余,注意单个0的输出
- #include
- using namespace std;
- int main()
- {
- int nums[101],len=-1,N,op;
- cin >> N >> op;
- do{
- nums[++len] = N%op;
- N/=op;
- } while (N);
-
- for(int z=0;z<=len;z++){
- if(nums[z]!=nums[len-z]) {
- cout << "No" << endl;
- break;
- }
- if(z==len) puts("Yes");
- }
-
- cout << nums[len];
- for(int z=len-1;z>=0;z--) cout << " " << nums[z];
- return 0;
- }