zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)

    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 b2, where it is written in standard notation with k+1digits ai​​ as (. Here, as usual, 0 for all i and ak​​ is non-zero. Then N is palindromic if and only if ai​​=aki​​ for all i. Zero is written 0 in any base and is also palindromic by definition.

    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 is the decimal number and 2 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 "ak​​ ak1​​ ... a0​​". Notice that there must be no extra space at the end of output.

    Sample Input 1:

    27 2
    

    Sample Output 1:

    Yes
    1 1 0 1 1
    

    Sample Input 2:

    121 5
    

    Sample Output 2:

    No
    4 4 1

    转换为某进制数后在判断是否为回文数
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <set>
     4 using namespace std;
     5 int a[10005];
     6 int n,m;
     7 void _deal(int x,int y)
     8 {
     9     int t=0;
    10     while(x){
    11         a[t++]=x%y;
    12         x/=y;
    13     }
    14     int flag=1;
    15     for(int i=0;i<=t/2;i++){
    16         if(a[i]!=a[t-i-1]){
    17             flag=0;
    18             break;
    19         }
    20     }
    21     if(flag) cout<<"Yes"<<endl;
    22     else cout<<"No"<<endl;
    23     for(int i=t-1;i>=0;i--){
    24         if(i==0) cout<<a[i]<<endl;
    25         else cout<<a[i]<<" ";
    26     }
    27 }
    28 int main()
    29 {
    30     while(cin>>n>>m){
    31         _deal(n,m);
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    【Spring Boot】关于上传文件例子的剖析
    GIT初始学习记录
    Java代码混淆工具ProGuard
    Kafka 概念、单机搭建与使用
    流网络分析系统-SNAS
    【Spring Boot】使用JDBC 获取相关的数据
    二叉树【按层打印、序列化、反序列化】
    跨域共享cookie
    启动kafka集群,关闭kafka集群脚本
    kafka-consumer.properties
  • 原文地址:https://www.cnblogs.com/wydxry/p/11158525.html
Copyright © 2011-2022 走看看