A1019. 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 the sum of (aibi) for i from 0 to k. 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 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 <= 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
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int N, b, m[], len = ;
int tag = ;
scanf("%d%d", &N, &b);
do{
m[len++] = N % b;
N = N / b;
}while(N != );
for(int i = , j = len - ; i <= j; i++, j--){
if(m[i] != m[j]){
tag = ;
break;
}
}
if(tag == )
printf("Yes\n");
else
printf("No\n");
printf("%d", m[len - ]);
for(int i = len - ; i >= ; i--)
printf(" %d", m[i]);
cin >>N;
return ; }
总结:
1、进制转换a进制转b进制时,先转为10进制,再转为b进制。将10进制转为b进制时采用除基取余法,将数字N不断除以基,余数作为结果的低位存储,商则循环除基,直到商为0。输出结果时,最后得到的为最高位。
2、除基取余法最好使用do while循环,因为有可能被除数为0。
A1019. General Palindromic Number的更多相关文章
- 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 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] A1019 General Palindromic Number
[题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...
- PAT A1019 General Palindromic Number (20 分)
AC代码 #include <cstdio> const int max_n = 1000; long long ans[max_n]; int num = 0; void change( ...
- 1019 General Palindromic Number (20 分)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...
- PAT1019:General Palindromic Number
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
随机推荐
- tomcat多实例方案启动脚本
批量启动 #!/bin/sh BASE_PATH="/usr/local/tomcat8/tomcat-ins/"bash $BASE_PATH/web1/tomcat.sh st ...
- 自动化批量管理工具salt-ssh - 运维小结
根据以往运维工作中操作经验来说,当管理上百台上千台服务器时,选择一款批量操作工具是及其有必要的.早期习惯于在ssh信任关系的前提下做for;do;done循环语句的批量操作,后来逐渐趋于使用批量工具操 ...
- Jmeter(GUI模式)教程
前些天,领导让我做接口的压力测试.What??我从未接触过这方面,什么都不知道,一脸蒙.于是我从学习jmeter开始入手. 现在记录下来jmeter的使用步骤,希望能对大家有所帮助. 一.安装Jmet ...
- 把cnblogs变成简书 - cnblogs博客自定义皮肤css样式
吐槽 博客园cnblogs作为老牌的IT技术博客类网站,为广大的开发者提供了非常不错的学习交流平台. 虽然博客内容才是重点,但是如果有赏心悦目的页面不更好吗! cnblogs可以更换博客模板,并且提供 ...
- Linux内核分析第六周总结
进程控制块PCB--task_struct 操作系统的内核里的三大功能: 进程管理 内存管理 文件系统 进程描述符--task_struct 进程管理是最核心的内容 然而Linux进程的状态与操作系统 ...
- github实验三结对报告
一.题目简介 本项目需要实现一个具有四则运算的计算器,能够实现基本的加.减.乘.除运算,以及其他的辅助功能(阶乘.正弦.余弦.指数运算):界面简洁实用,模拟Windows中的计算器程序,要提供主要的设 ...
- 阅读<构建之法>13、14、15、16、17章
13章 这么多测试为什么不能整理出一个包括所有功能的测试呢?看着那么多测试都感觉奇怪了. 14章 怎样才能体现一个测试人员的工作价值呢?这样的判断又是否会太独断了? 15章 在时间上,会不会因不同功能 ...
- Magazine Ad CodeForces - 803D (二分+贪心)
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- Delphi处理Http请求自定义Header
在HTTP请求中,get方法是默认的,但在URL地址长度是有限的,请求方法能传送的数据也是有限的,一般get方法传递的数据不能大于2KB,当get请求方法传递的数据长度不能满足需求时,就需要采用另一种 ...
- Windows 安装 docker 以及1709的简单使用
PS C:\> Install-Module -Name DockerMsftProvider -Repository PSGallery -Force PS C:\> Install-P ...