PAT 甲级 1019 General Palindromic Number
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
#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的更多相关文章
- PAT 甲级 1019 General Palindromic Number(20)(测试点分析)
1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...
- PAT 甲级 1019 General Palindromic Number(简单题)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- 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 ...
- 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 ...
- 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 ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT 1019 General Palindromic Number
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
随机推荐
- ubuntu apt源配置
前言:看见Ubuntu新出了18.04版本感觉不错,装一个玩玩,虽然有很多教程可以参考,但我也给出一个不是很一样的方案吧,尽量解释的详细一点. 为了下载更方便,速度更快,我们往往在使用Linux系列系 ...
- 转:c# Linq 的分页[转]
转:http://www.cnblogs.com/leleroyn/archive/2008/05/14/1196811.html 很多学习Linq的朋友肯定有自己所不同的方法,考虑这个问题我所想到的 ...
- C语言复习20170728
C语言复习20170728 键盘输入和屏幕输出 字符常量:把字符放在一对单引号内,适用于多数可打印字符. 转义字符: 以反斜线()开头,也是放在一对单引号内,适用于控制字符. .\t,是水平制表符,相 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- 06004_Redis的启动、使用和停止
1.Redis的启动 (1)前端模式启动 ①直接运行bin/redis-server将以前端模式启动:切换到 /usr/local/redis/bin目录下,然后./redis-server : ②前 ...
- C#目录:藏锋
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 此为C#专题的分类,只会记录我对于C#一些需求的解决方案,并非全部学习资料(全部可以参考微软提供的API) 主 ...
- 【MySQL高级特性】高性能MySQL第七章
2017-07-25 14:15:43 前言:MYSQL从5.0和5.1版本开始引入了很多高级特性,例如分区.触发器等,这对有其他关系型数据库使用 背景的用户来说可能并不陌生.这些新特性吸引了很多用户 ...
- MQ配置安装
一,MQ安装 ./mqlicense.sh -accept rpm -ivh MQSeries*.rpm -- rpm -qa|grep MQSeries 二,MQ配置 环境变量配置(MQM)实际安 ...
- 函数parseQuery用于解析url查询参数
在百度上找的,以后忘了再看. 语法如下: var obj = parseQuery(query) query是被解析的查询参数,函数返回解析后的对象. 使用范例如下: var jerry = pars ...
- 会声会影2018提示dll文件丢失怎么办?
一些会声会影2018用户,在安装.使用软件的过程中,会出现dll缺失的提示,导致软件无法打开,那么,出现这一问题要怎么解决.接下来小编为大家具体介绍下两种解决方法. 图1:dll丢失提示 打开会声会影 ...