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 a​i​​ as ∑​i=0​k​​(a​i​​b​i​​). Here, as usual, 0≤a​i​​<b for all i and a​k​​ is non-zero. Then N is palindromic if and only if a​i​​=a​k−i​​ 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<N≤10​9​​ is the decimal number and 2≤b≤10​9​​ 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 "a​k​​ a​k−1​​ ... a​0​​". 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

鸣谢网友“CCPC拿不到牌不改名”修正数据!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = ;
int s[maxn] = { }, s2[maxn] = { };
int main() {
int n, b;
cin >> n >> b; if (n == ) {
printf("Yes\n0");
return ;
}
int i = ;
while (n != ) {
s[i] = n % b;
s2[i] = s[i];
n /= b;
i++;
}
reverse(s, s+i);
int flag = ;
for (flag; flag < i; flag++) {
if (s[flag] != s2[flag]) {
break;
}
}
if (flag == i) printf("Yes\n");
else printf("No\n"); int j;
for (j = ; j < i-; j++) {
printf("%d ", s[j]);
}
printf("%d", s[i-]);
system("pause");
}

注意点:不能用string来做,因为b可能会很大,用string无法存储太大的数字,最多就255,还是要开一个int数组。一开始觉得用string超容易,只要存进来然后reverse一下直接==比较就ok了,发现有两个测试点一直过不了,难。

PAT A1019 General Palindromic Number (20 分)——回文,进制转换的更多相关文章

  1. PAT A1019 General Palindromic Number (20 分)

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

  2. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  3. 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 Nu ...

  4. PAT Advanced 1019 General Palindromic Number (20 分)

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

  5. 【PAT甲级】1019 General Palindromic Number (20 分)

    题意: 输入两个正整数n和b(n<=1e9,2<=b<=1e9),分别表示数字的大小和进制大小,求在b进制下n是否是一个回文串,输出“Yes”or“No”,并将数字n在b进制下打印出 ...

  6. [PAT] A1019 General Palindromic Number

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

  7. 1019 General Palindromic Number (20 分)

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

  8. PAT 1019 General Palindromic Number[简单]

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

  9. pat1019. General Palindromic Number (20)

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

随机推荐

  1. quartz部署出现找不到表的情况,错误提示: Table 'heart_beat.QRTZ_LOCKS' doesn't exist

    描述一下,本地可以,部署到Linux就不行,Linux上的数据库是本地直接拷贝上去的,项目环境是Spring Boot2.1.Shiro.MyBatis.Redis.swagger.Bootstrap ...

  2. 【github&&git】7、gitignore 修改不起作用

    在git使用过程中有时会遇到修改了.gitignore文件,修改了之后发现,不能起作用,这是因为git存在缓存问题,所以做一下步骤即可: git rm -r --cached . git add . ...

  3. 【IDEA&&Eclipse】3、IntelliJ IDEA 的 20 个代码自动完成的特性

    在这篇文章中,我想向您展示 IntelliJ IDEA 中最棒的 20 个代码自动完成的特性,可让 Java 编码变得更加高效.对任何集成开发环境来说,代码的自动完成都是最最重要的一项功能,它根据你输 ...

  4. Java static 语句块

    总结前一天学习,参考原文http://www.cnblogs.com/dolphin0520/p/3799052.html1: 对Static有了进一步的认识    这个地方重点是初始化各个变量顺序, ...

  5. javascript之揭示模式

    一.该模式优缺点1.优点:该模式可以使脚本语法更加一致,在模块代码底部,它很容易指出哪些函数和变量可以被公开访问,从而改善可读性. 2.缺点:如果一个私有函数引用一个公有函数,公有函数是不能被覆盖的. ...

  6. 初学HTML-7

    表单中的一些标签 label标签:让文字和输入框进行绑定,即,点击文字,输入框可以开始输入(默认文字和输入框没有关系,不会聚焦) 格式:<form action=""> ...

  7. 前端打包文件在 nginx 上 403 的解决办法

    遇到的问题表现出来就是如题所述,因此作为题目. 我们知道,前端有很多方便的构建和打包工具,如 webpack 等,通常我们会把前端文件打包到dist目录下,部署到服务器上,如 nginx 等. 这次遇 ...

  8. Runtime和Process

    private void runByshcommand(String command) { try { System.out.println("开始执行命令....."); Pro ...

  9. 【代码笔记】Web-HTML-简介

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. Tomcat 8默认工具manager管理页面访问配置

    Tomcat 8默认工具manager管理页面访问配置 1. 分配相关的角色权限 需要配置的配置文件是${catalina.home}/conf/tomcat-users.xml先给Tomcat访问相 ...