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 ...
随机推荐
- python自动化学习
1.环境搭建 1.1 下载或拷贝整个PyAuthoTest到指定的目录,如D:\PyAuthoTest 1.2 安装Python2.7以及需要使用到的安装包列表如下 requests-2.10.0.t ...
- C#——数据库取数据,DataGridView显示数据
使用未封装的方法连接数据库 步骤: 一.确定连接方式(以SqlServer为例): ①Windows身份验证. string ConnectionType = "server=.;datab ...
- 20155307 《Java程序设计》课堂实践项目数据库
老师我上课没有在规定时间内完成,这是我补交的截图
- 程序与程序之间共享对象:MarshalByRefObject
源自于:http://stackoverflow.com/questions/439173/message-pumps-and-appdomains/442316 程序与程序之间共享对象: Marsh ...
- [arc067F]Yakiniku Restaurants[矩阵差分]
Description 传送门 Solution 假如我们确定了烧烤店区间[l,r],则票j必定会选择在B[i][j](l<=i<=r)最大的烧烤店使用. 反过来想,我们想要票j在第i个烧 ...
- 苏州Uber优步司机奖励政策(4月11日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(4月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【SQLSERVER】递归查询算法实例
一.递归查询 1.结构: 递归CTE最少包含两个查询(也被称为成员). 第一个查询为定点成员,定点成员只是一个返回有效表的查询,用于递归的基础或定位点. 第二个查询被称为递归成员,使该查询称为递归成员 ...
- 用反射或委托优化switch太长的方法
在代码进行优化的时候,发现了switch case太长,有的竟然长达30个远远超过一屏这样在代码的可读性来说很差.特别在我们看代码的时候要拉下拉框我个人觉得这是不合理的.但是我不建议有switch就进 ...
- TeamViewer卡在正在初始化显示参数
在windows的mstsc远程桌面中打开teamviewer,远程桌面开着的时候可以连接teamviewer,但是当我断开mstsc之后,再用teamviewer连就连接不上了,一直都是正在初始化显 ...