https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984

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 the sum of (a~i~b^i^) for i from 0 to k. 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 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 <= 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
 
题解: char 能存 300左右 ; int -2^31, 2^31 - 1 ; long long 存不到20位 (-2^63, 2^63 - 1), 所以不能用 char
代码:

#include <bits/stdc++.h>
using namespace std; int num[1111], out[1111]; int main() {
int n, d;
scanf("%d%d", &n, &d);
if(n == 0) {
printf("Yes\n0\n");
return 0;
} int cnt = 0;
while(n != 0) {
num[cnt ++] = n % d;
n /= d;
} for(int i = 0; i < cnt; i ++) {
out[i] = num[i];
}
for(int i = 0; i <= cnt / 2 - 1; i ++)
swap(num[i], num[cnt - 1 - i]);
//cout<<num<<endl;
//cout<<out<<endl; int flag = 1;
for(int i = 0; i < cnt; i ++) {
if(out[i] != num[i]) flag = 0;
}
if(flag) {
printf("Yes\n");
for(int i = cnt - 1; i >= 0; i --) {
printf("%d", num[i]);
printf("%s", i != 0 ? " " : "\n");
}
}
else {
printf("No\n");
for(int i = cnt - 1; i >= 0; i --) {
printf("%d", out[i]);
printf("%s", i != 0 ? " " : "\n");
}
}
return 0;
}

  

PAT 甲级 1019 General Palindromic Number的更多相关文章

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

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

  2. PAT 甲级 1019 General Palindromic Number(简单题)

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

  3. PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

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

  4. PAT甲级——1019 General Palindromic Number

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

  5. 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 ...

  6. 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 ...

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

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

  8. PAT 1019 General Palindromic Number

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

  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. Python学习:18.Python异常处理

    一.为什么使用异常处理 当程序运行的时候出现了异常,导致程序终止运行,为了解决这种情况,我们需要预先对可能出现的异常进行处理,一旦出现这种异常,就使用另一种方式解决问题,还有就是错误信息是使用者没有必 ...

  2. MongoDB分片介绍

    本文简单介绍MongoDB的分片功能,对分片进行了概述,具体的功能详解,后续文章会陆续推出 分片是把数据分配到多个服务器上的一种方式,MongoDB使用分片实现大数据部署以及高吞吐操作. 大数据以及高 ...

  3. 转 关于window10安装jdk,配置环境变量,javac不是内部或外部命令,也不是可运行的程序 或批处理文件的细节问题。

    今日拿到一台新的window10笔记本电脑,非常熟练的安装了JDK(因为在学校经常给同学安装JDK - -)但是发现java java -version命令都可以使用,唯独javac命令出现不是内部或 ...

  4. Angular5学习笔记 路由配置

    因为angular-cli脚手架的关系,所以配置路由可以通过命令行来创建路由文件 原本创建一个angular项目的命令是ng new 项目名,就可以了,但这样创建出来的项目是没有路由文件的. 如果需要 ...

  5. 20155220 实验三 敏捷开发与XP实践 实验报告

    20155220 实验三 敏捷开发与XP实践 实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...

  6. 2016-2017-2 20155302 实验四 Android 开发基础

    2016-2017-2 20155302 实验四 Android 开发基础 实验内容 1.下载和安装Android Studio 2.学会使用Android Studio进行简单的Android开发 ...

  7. # 20155308 2016-2017-2《Java程序设计》课堂实践项目 5月17日

    20155308 2016-2017-2<Java程序设计>课堂实践项目 5/17 本次因为git出现了问题,所以没有按时提交我的代码 问题一 在IDEA中对P145 MathTool.j ...

  8. [BZOJ4011][HNOI2015]落忆枫音-[dp乱搞+拓扑排序]

    Description 传送门 Solution 假如我们的图为DAG图,总方案数ans为每个点的入度In相乘(不算1号点).(等同于在每个点的入边选一条边,最后一定构成一棵树). 然而如果加了边x- ...

  9. 【LG3232】[HNOI2013]游走

    题面 洛谷 题解 代码 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstr ...

  10. cogs1439 货车运输

    cogs1439 货车运输 一道傻逼板子题. 边一定在最大生成树上,这个可以用消圈证明 然后kruskal跑一遍再搜一遍再建ST表再跑LCA这题就做完了. RT PS.交上去的代码把Kruskal打成 ...