1019 General Palindromic Number(进制转化,回文数)

缺乏、安全感 2024-04-08 09:36 149阅读 0赞

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:

  1. 27 2

Sample Output 1:

  1. Yes
  2. 1 1 0 1 1

Sample Input 2:

  1. 121 5

Sample Output 2:

  1. No
  2. 4 4 1

1、大致题意

给出一个 10进制数 和 另一个基数b,问这个 10进制 数转化为 基数b 的数值是不是回文数

2、基本思路

简单题

3、AC代码

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int N,b;
  5. vector<int> a;
  6. void init(int N) {
  7. int n=N,k;
  8. while(n!=0) {
  9. k=n%b;
  10. n/=b;
  11. a.push_back(k);
  12. }
  13. }
  14. int is_Palindromic() {
  15. int size=a.size();
  16. for(int i=0; i<size/2; i++) {
  17. if(a[i]!=a[size-i-1]) {
  18. return -1;
  19. }
  20. }
  21. return 1;
  22. }
  23. void print() {
  24. int size=a.size();
  25. cout<<a[size-1];
  26. for(int i=size-2; i>=0; i--) {
  27. cout<<" "<<a[i];
  28. }
  29. }
  30. int main() {
  31. cin>>N>>b;
  32. init(N);
  33. if(is_Palindromic()==1) {
  34. cout<<"Yes"<<endl;
  35. print();
  36. } else {
  37. cout<<"No"<<endl;
  38. print();
  39. }
  40. return 0;
  41. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,149人围观)

还没有评论,来说两句吧...

相关阅读