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 b >= 2, where it is written in standard notation with k+1 digits ai as the sum of (aibi) for i from 0 to k. Here, as usual, 0 <= ai < b for all i and ak is non-zero. Then N is 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 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 non-negative numbers N and b, where 0 <= N <= 109 is the decimal number and 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 "ak ak-1 ... 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
 #include<cstdio>
#include<iostream>
using namespace std;
int main(){
int N, b, m[], len = ;
int tag = ;
scanf("%d%d", &N, &b);
do{
m[len++] = N % b;
N = N / b;
}while(N != );
for(int i = , j = len - ; i <= j; i++, j--){
if(m[i] != m[j]){
tag = ;
break;
}
}
if(tag == )
printf("Yes\n");
else
printf("No\n");
printf("%d", m[len - ]);
for(int i = len - ; i >= ; i--)
printf(" %d", m[i]);
cin >>N;
return ; }

总结:

1、进制转换a进制转b进制时,先转为10进制,再转为b进制。将10进制转为b进制时采用除基取余法,将数字N不断除以基,余数作为结果的低位存储,商则循环除基,直到商为0。输出结果时,最后得到的为最高位。

2、除基取余法最好使用do while循环,因为有可能被除数为0。

A1019. General Palindromic Number的更多相关文章

  1. PAT A1019 General Palindromic Number (20 分)——回文,进制转换

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  2. PAT甲级——A1019 General Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  3. [PAT] A1019 General Palindromic Number

    [题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...

  4. PAT A1019 General Palindromic Number (20 分)

    AC代码 #include <cstdio> const int max_n = 1000; long long ans[max_n]; int num = 0; void change( ...

  5. 1019 General Palindromic Number (20 分)

    1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...

  6. PAT1019:General Palindromic Number

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  7. PAT 1019 General Palindromic Number

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  8. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  9. PAT 1019 General Palindromic Number[简单]

    1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...

随机推荐

  1. Socket异步通信及心跳包同时响应逻辑分析(最后附Demo)。

    有段时间没有更博了,刚好最近在做Socket通信的项目,原理大致内容:[二维码-(加logo)]-->提供主机地址和端口号信息(直接使用[ThoughtWorks.QRCode.dll]比较简单 ...

  2. Jvm远程监控

    服务器运行新建文件 : udi.policy grant codebase "file:${java.home}/../lib/tools.jar" { permission ja ...

  3. Linux下通过受限bash创建指定权限的账号

    在日常业务运维中,有时为了配合解决问题,需要给非运维人员开通系统账号,用于查询日志或代码.通常为了系统安全或避免不必要的误操作等目的,会将账号权限降至最低.下面介绍下在Linux下通过受限bash创建 ...

  4. Centos下MooseFS(MFS)分布式存储共享环境部署记录

    分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,分布式文件系统的实际基于客户机/服务器模式.目前 ...

  5. C_数据结构_快速排序

    # include <stdio.h> void QuickSort(int * a, int low, int high); int FindPos(int * a, int low, ...

  6. android studio报Resolved versions for app (26.1.0) and test app (27.1.1)differ. 错误的解决办法

    https://blog.csdn.net/qq_36636969/article/details/80278150

  7. C++Primer中for(auto it=s.cbegin(); iter!=s.cend() && !it->empty(); ++it){ cout<<*it<<endl; }有错误

    在C++ Primer 中文版 第五版的 98页 ,有这么一段代码 for(auto it=text.cbegin(); it!=text.cend() && !it->empt ...

  8. solt插槽的使用。

    在组件内template中使用 <slot name='header'></slot> 在页面内 直接添加标签 如 <hs><h1 slot='header' ...

  9. Delphi编码规范

    以下是在之前的公司与别人共同参与制定的Delphi开发规范(5年前了,那时用的还是Delphi7,部分规则可能有些过时,但不整理了,觉得有用的人,自己整理成适合自己的开发规范吧), 目    录一.序 ...

  10. Jenkins之Linux和window配置区别

    一.命令行配置 windows: java -jar .\libs\gen-html-report-1.0-SNAPSHOT.jar .\reports_%BUILD_NUMBER%.html .\t ...