PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)
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 (. Here, as usual, 0 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 is the decimal number and 2 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
解题思路
题目大意:给定一个N和b,求N在b进制下,是否是一个回文数(Palindromic number)。其中,0<N,b<=10^9。
使用int作为基本类型就足够了,然后进行进制转换,需要注意的是,b进制比较大,可能得到的不是一个单位数,比如16进制下的10~15。可以用char进行存储,然后比较。或者用一个的二维数组进行比较也行。
但是后来发现用char,数字+'0'可能会超出ascii表示的范围,有一个测试点就是过不去。
最终选择用vector<int>,很方便。
典型测试样例:
Yes
No
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int d,b;
cin>>d>>b;
char s[];
int k=;
while(d>){
s[k++]=d%b+'';
//cout<<d%b<<endl;
d=d/b;
}
k--;
//cout<<d<<endl;
int middle=int((+k)/);
int f=;
for(int i=;i<=middle;i++){
if(s[i]!=s[k+-i]){
f=;
break;
}
}
if(f){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
for(int i=k;i>=;i--){
cout<<s[i]-'';//可能b的进制比较大,所以要-'0'输出
if(i!=){
cout<<" ";
}
}
return ;
}
PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)的更多相关文章
- 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
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
https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 A number that will be ...
- 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 ...
- 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 (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)(20 分) A number that will be the same when it is written forward ...
随机推荐
- [转载]npm 与 package.json 快速入门教程
npm 与 package.json 快速入门教程 2017-08-02 19:16:20 拭心 阅读数 78648更多 分类专栏: 学学前端 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
- Kafka 消息队列系列之分布式消息队列Kafka
介绍 ApacheKafka®是一个分布式流媒体平台.这到底是什么意思呢?我们认为流媒体平台具有三个关键功能:它可以让你发布和订阅记录流.在这方面,它类似于消息队列或企业消息传递系统.它允许您以容 ...
- [Luogu] 开关
https://www.luogu.org/problemnew/show/P3870 线段树区间翻转 + 区间查询 #include <iostream> #include <cs ...
- windows游戏编程键盘
键盘 首先我们来看一下键盘常用消息 键盘的消息处理过程 键盘消息会有击键消息和字符消息 键盘消息的附加参数 wParam:非系统键的虚拟码 lParam: windows常用虚拟键码及对应的按键
- tesseract 安装及使用
安装软件 tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/ 安装即可! 安装完成tesseract-ocr后,需要做一下配置 . 在P ...
- Prometheus HA详解
Prometheus 横向扩展 当Exporter或者采集信息需要越来越多时就会考虑高可用,高可用优点不会因为集群中某个节点down而导致Prometheus不可用,可以让算力下沉; 缺点是A-Pro ...
- loadrunner11安装
今天虚拟机里面装了下lr11,虚拟机版本是vm9.0,先在虚拟机里面装了windows2003,当然lr也是可以装在自己电脑上面的,但是最好是纯净的环境,由于我电脑东西比较多,所以我就装在虚拟机里面了 ...
- java spring boot- freemarker 配置 yml使用流程
1.pom.xml 加入maven 依赖 <!-- 引入 freemarker 模板依赖 --><dependency> <groupId>org.springf ...
- JavaWeb_(Mybatis框架)MyBatis Generator简单入门
官方文档 传送门 下载地址 传送门 MyBatis Generator(MBG)简介: MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器.它将为 ...
- 5.3.4 Hadoop序列化框架
序列化框架 除了writable实现序列化之外,只要实现让类型和二进制流相互转换,都可以作为hadoop的序列化类型,为此Hadoop提供了一个序列化框架接口,他们在org.apache.hadoop ...