1019. General Palindromic number

Source: Internet
Author: User

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

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.