1019. General Palindromic number (20)

Source: Internet
Author: User

Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Procedures for the award of questions StandardAuthor Chen, Yue

A number that would be the same when it was written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. All digit numbers is palindromic numbers.

although palindromic numbers is most often considered in the decimal system, the concept of palindromicity can Applie D to the natural numbers in any numeral system. Consider a number N > 0 in base b >= 2, where it's written in standard notation with k+1 digits ai as the sum of (ai) for I from 0 to K. Here, as usual, 0 <= ai < b for all I and ak is Non-zero. Then N are palindromic if and only if Ai = ak-i for All I. Zero is written 0 in any base and is also palindromic by definition.

Given any non-negative decimal integer N and a base B, you were supposed to tell if N is a palindromic number in base B.

Input Specification:

Each input file contains the one test case. Each case consists of a non-negative numbers n and b, where 0 <= n <= 9 is the decimal number and2 <= b <= 9 is thebase. The numbers is separated by a space.

Output Specification:

For each test case, the first print in the one line "Yes" if N is a palindromic number in base B, or "No" if not. Then on the next line, print N as the number in base B in the form "ak ak-1 ... a0". Notice that there must is no extra space at the end of output.

Sample Input 1:
27 2
Sample Output 1:
Yes1 1 0 1 1
Sample Input 2:
121 5
Sample Output 2:
No4 4 1
Source: >  

  
 
  1. #include<iostream>
  2. #include<vector>
  3. #include<stdio.h>
  4. #include <algorithm>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. vector<int> num;
  8. bool CheckSeq() {
  9. Span class= "KWD" >for ( int i = 0 I < num size () / 2 + 1 I ++) {
  10. if (num[i] != num[num.size() - i - 1])
  11. return false;
  12. }
  13. return true;
  14. }
  15. int main(void) {
  16. int n, k;
  17. cin >> n >> k;
  18. int yushu;
  19. while (true)
  20. {
  21. yushu = n%k;
  22. num.push_back(yushu);
  23. n = n / k;
  24. if (n == 0)
  25. break;
  26. }
  27. reverse(num.begin(), num.end());
  28. if (CheckSeq() == false) {
  29. cout << "No" << endl;
  30. }
  31. else
  32. cout << "Yes" << endl;
  33. for (int i = 0; i < num.size(); i++) {
  34. cout << num[i];
  35. if (i != num.size() - 1)
  36. cout << " ";
  37. }
  38. return 0;
  39. }



From for notes (Wiz)

1019. General Palindromic number (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.