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 applied 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 (aIBi) for i from 0 to K. Here, as usual, 0 <= a< b for all I and ak are non-zero. Then N was palindromic if and only if ai = ak-i for all I. Written 0 in any base and are 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.
#include <stdio.h> #include <stdlib.h> #include <vector>using namespace Std;int main () { long n,b; scanf ("%ld%ld", &n,&b); vector<int> revers; int i=0; if (n<=1) { printf ("yes\n"); printf ("%d\n", n); return 0; } while (n!=0) { revers.push_back (n%b); n=n/b; } BOOL Flag=true; int length=revers.size (); for (i=0;i<length/2;i++) { if (revers[i]!=revers[length-i-1]) { printf ("no\n"); Flag=false; break; } } if (flag) printf ("yes\n"); for (i=length-1;i>0;i--) printf ("%d", revers[i]); printf ("%d\n", Revers[i]); System ("pause"); return 0; }
1019. General Palindromic number