PTA (Advanced Level) 1019 General Palindromic Number
General Palindromic Number
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, 0for 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,要求将b转化为b进制后,判断转化完成的数字是否为回文数字。
我们可以写一个函数,将十进制数字n转化为b进制并将其每一位都记录在一个容器num中,之后从num第一位开始到中心为止,判断该位与其对称位置是否全相等即可。若全相等,则目标数字是回文数字,否则不是回文数字。
AC代码
#include <bits/stdc++.h>
using namespace std;
vector<int> num; //用来储存进制转换后数字的容器
void changeRadix(int n, int radix){ //进制转换函数
while(n){
int temp;
temp = (n % radix);
num.push_back(temp);
n /= radix;
}
}
bool judge(){ //回文判断函数
int len = num.size();
for(int i = ; i < len / ; i++){
if(num[i] != num[len - i - ]) //判断该位置与其对称位置是否相等
return false;
}
return true;
}
int main()
{
int n, b;
while(scanf("%d%d", &n, &b) != EOF){ //输入数字n与目标进制b
if(n == ){ //0的所有进制都是回文数
printf("Yes\n");
printf("0\n");
continue;
}
num.clear(); //清空容器num
changeRadix(n, b); //将n转化为b进制
if(judge()){ //判断转换进制后是否为回文数字
printf("Yes\n"); //是则输出Yes
//由于我们是逆序储存转换后的数字的,所以在输出前要将其反转
reverse(num.begin(), num.end());
bool flag = false;
for(auto i : num){ //输出转换后数字
if(flag)
printf(" ");
else
flag = true;
cout << i;
}
printf("\n");
}else{ //不是回文数字输出No,其余操作同上
printf("No\n");
reverse(num.begin(), num.end());
bool flag = false;
for(auto i : num){
if(flag)
printf(" ");
else
flag = true;
cout << i;
}
printf("\n");
}
}
return ;
}
PTA (Advanced Level) 1019 General Palindromic Number的更多相关文章
- PAT (Advanced Level) 1019. General Palindromic Number (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 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(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)(20 分) A number that will be the same when it is written forward ...
- 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 ...
随机推荐
- Postgresql 日志审计
配置log_destination = 'csvlog'logging_collector = off log_directory = 'pg_log'log_connections = onlog_ ...
- PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码
一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...
- [51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯
>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013. ...
- 深度学习之TensorFlow的介绍与安装
TensorFlow是一个采用数据流图(data flow graphs)用于数值计算的开源软件库.它最初是由Google大脑小组的研发人员设计开发的,用于机器学习和神经网络方面的研究.但是这个系统的 ...
- 记在WEBAPI中AutoMapper的初使用方法
很早就听说AutoMapper了.这些天一直在写api接口,发现之前的类型转换有点问题,就想到了用AutoMapper.用作DTO转换工具. 废话不多说,直接开些代码 首先 在Vs中找到 工具--- ...
- CTF之信息泄漏
web源码泄漏 .hg源码泄漏: 漏洞成因:hg init的时候会生成.hg,http://www.xx.com/.hg/, 工具:dvcs-ripper,(rip-hg.pl -v -u http ...
- 学习笔记|Java 教程|菜鸟教程
一.Java 教程 1.Java 教程 Java 在线工具 JDK 1.6 在线中文手册 我的第一个 JAVA 程序 创建文件 HelloWorld.java(文件名需与类名一致), 代码如下: pu ...
- 喝最烈的酒、挖最大的DONG——工具与技巧篇
本文作者:i春秋签约作家——黑色镰刀 0×00 前言 在这个科技发达的时代,很多时候工具都可以代替人做很多事情,之前我就有谈起过有企业将人工智能运用于网络安全方面,像如今,也有更多更人性化更智能的工具 ...
- [RHEL] 配置 LVM 卷
[RHEL] 配置 LVM 卷 一.Introduction 基础预览 :LVM 认知与扩容操作 高端实战:Linux系统如何迁移至LVM磁盘 之前转过一篇文章 LVM分区在线扩容 ,其原因是我需要给 ...
- 总结day7 ---- 函数的内容 ,初识,返回值,进阶(一)
内容大纲: 一: 函数识别 二: 函数的结构 三: 函数的返回值, 四: 函数的参数 五: 动态参数 六: 形参的顺序 七: 名称空间 八: 作用域 九: 加载顺序和取值顺序 十: 内置函数 十一: ...