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学习之——Python安装

    环境:Centos6.5+python2.7.5 1.centons6.5系统中是已经安装了python的,先查看版本是不是需要的 python --version 2.安装一些必要的包,防止后面需要 ...

  2. 我的职业规划(android)

    通过一段时间的想法,自己大概圈定了自己的未来三年的职业规划,关于android的,希望大家多多批评,多多指教.或者大家也能讨论下自己对于未来的期许或者路线,虽然每个人都有自己自身的情况,但是总会有一些 ...

  3. 折腾VIM的C++缩进

    自己是2014年的时候,开始学习VIM编辑器.记得当时把整个VIM入门手册几乎通读了一边,为其强大的功能和便捷的操作所折服. 今天再次捣鼓了以下VIM,只因为用VIM编辑C++的代码时,类中的publ ...

  4. 20155206 2016-2017-2 《JAVA程序设计》 第二周学习总结

    20155206 2016-2017-2<JAVA程序设计>第二周学习总结 教材学习内容总结 类型 整数 字节 浮点数 字符 布尔 变量 变量在命名时,不可以使用数字或一些特殊字符:*.& ...

  5. 20155222 2016-2017-2 《Java程序设计》实验三

    20155222 2016-2017-2 <Java程序设计>实验三 1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单 ...

  6. 实验一 Java开发环境的熟悉(Linux+Eclipse)

    实验一 Java开发环境的熟悉(Linux+Eclipse) 在Linux或Window或macOS中命令行下运行Java 在Linux 或Window或 macOS环境中 IDEA中调试设置断点 实 ...

  7. vim 多个文件切换

    打开多个文件:1.vim还没有启动的时候:在终端里输入 vim file1 file2 ... filen便可以打开所有想要打开的文件2.vim已经启动输入:open file可以再打开一个文件,并且 ...

  8. tkinter菜单图标,工具栏

    所用的图片: import tkinter as tk from tkinter import messagebox, filedialog, simpledialog, colorchooser f ...

  9. JS基础,课堂作业,三个数字排序

    三个数字大小排序 <script> var a = parseInt(prompt("请输入第一个整数:")); var b = parseInt(prompt(&qu ...

  10. Java or Python?测试开发工程师如何选择合适的编程语言?

    很多测试开发工程师尤其是刚入行的同学对编程语言和技术栈选择问题特别关注,毕竟掌握一门编程语言要花不少时间成本,也直接关系到未来的面试和就业(不同企业/项目对技术栈要求也不一样),根据自身情况做一个相对 ...