PAT A1019 General Palindromic Number (20 分)——回文,进制转换
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 ai as ∑i=0k(aibi). Here, as usual, 0≤ai<b for all i and ak is non-zero. Then N is palindromic if and only if ai=ak−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≤109 is the decimal number and 2≤b≤109 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 "ak ak−1 ... a0". 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 分)——回文,进制转换的更多相关文章
- PAT A1019 General Palindromic Number (20 分)
AC代码 #include <cstdio> const int max_n = 1000; long long ans[max_n]; int num = 0; void change( ...
- 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 (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 ...
- 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甲级】1019 General Palindromic Number (20 分)
题意: 输入两个正整数n和b(n<=1e9,2<=b<=1e9),分别表示数字的大小和进制大小,求在b进制下n是否是一个回文串,输出“Yes”or“No”,并将数字n在b进制下打印出 ...
- [PAT] A1019 General Palindromic Number
[题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...
- 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 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- pat1019. General Palindromic Number (20)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- 【CF932E】Team Work(第二类斯特林数)
[CF932E]Team Work(第二类斯特林数) 题面 洛谷 CF 求\(\sum_{i=1}^nC_{n}^i*i^k\) 题解 寒假的时候被带飞,这题被带着写了一遍.事实上并不难,我们来颓柿子 ...
- 【读书笔记】iOS-iCloud介绍
iCloud是一种面向消费者市场的云存储服务,苹果公司已经做了大量的工作让用户能够平滑过渡到iCloud,不过对开发者而言这意味着新的负担. 怎样使用iCloud? 你可以使用2种方式在你的应用中使用 ...
- 《Inside C#》笔记(十四) 反射
通过反射可以在运行时动态地获取一个应用的元数据. 一 反射相关的类和方法 与反射相关的类处在System.Reflection命名空间下,包括Assembly.Module.MethodInfo.Fi ...
- linux(乌班图)修改apt下载源
有时候会出现乌班图系统刚安装,无法使用apt下载安装软件工具,此时需要修改apt下载源. 1.进入/etc/apt/目录下 2.备份sources.list文件(如果不在root用户下,需在前面加s ...
- 小技巧-mac修改finder菜单栏
效果: 方法: 添加:打开finder后,长按command,可以将其他app拖到菜单栏. 删除:同理,长按command,将不需要的图标拖出菜单栏即可. PS:强烈推荐gotoshell这个小工具, ...
- TensorFlow深度学习入门
# -*- coding: utf-8 -*- """ Created on Tue Oct 2 15:49:08 2018 @author: zhen "&q ...
- java面试题之----spring MVC的原理和MVC
1.什么是mvc? 1.1原始比较初级的设计模式: 1.2 MVC设计模式 2MVC设计模式的优势与核心在于其能解耦和: 传统的设计模式相当于是一个串联的设计,只要其中一个环节出了问题便会使下一环节中 ...
- 百度纯CSS生成菜单
首页我们打看dreamweaver或其它编辑器,创建一个名为nav的导航菜单 <div class="nav"> <ul> <li><a ...
- weblogic CVE-2018-2628漏洞利用工具
weblogic CVE-2018-2628漏洞利用 漏洞环境: Windows2018R2 weblogic10.3.6 漏洞利用过程: 搭建好存在CVE-2018-2628漏洞的weblogic平 ...
- Shell脚本应用(if语句的结构)
1.测试:检测表达式是否成立,成立则返回值为0,否则为非0 方法: 1)test 表达式 2)[ 表达式 ] 2.文件测试: -d:是否为目录 -f:是否为文件 -e:是否存在 -r:是否有读取权限 ...