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 ...
随机推荐
- GlusterFS分布式存储数据的恢复机制(AFR)的说明
GlusterFSFS恢复数据都是基于副本卷来说的,GlusterFSFS复制卷是采用镜像的方式做的,并且是同步事务性操作.简单来说就是,某一个客户要写文件时,先把这个文件锁住,然后同时写两个或多个副 ...
- hdu 3038 给区间和,算出多少是错的
参考博客 How Many Answers Are Wrong Problem Description TT and FF are ... friends. Uh... very very good ...
- 12.9 Daily Scrum
在一些实现上,开发人员提出了意见,经过讨论后,我们决定取消“推荐餐厅”的功能,增加了“菜谱分类”的功能. 同时更新了相关人员的任务. Today's Task Tomorrow's Task 丁辛 ...
- android——error opening trace file: No such file or directory (2)
1.疑惑: 程序运行起来的时候日志总是显示下面这个错误,但是不影响程序的正常进行,我是用真机来测试的,android4.4.4(API17). 02-11 14:55:03.629 15525-155 ...
- 注解Annotation
@java.lang.annotation.Target(value={java.lang.annotation.ElementType.TYPE}) @java.lang.annotation.Re ...
- (Alpha)Let's-版本发布说明
我们的Let’s APP发布了! (下载地址在“下载与安装”部分) Alpha版本功能 Alpha版本是我们发布的第一个版本,所以仅实现了活动实体和用户实体之间的基础联系功能. 基本功能 登录和注册 ...
- 『编程题全队』Beata阶段项目复审
小组的名字和链接 优点 缺点,bug 报告(部分包括建议) 最终名次 想不出队名 1. 界面简洁大方2. 有搜索功能 1. 已经完成的活动缺了点提示界面2. 似乎界面有一点点卡顿目标实现:基本实现找到 ...
- auto_increment 自增键的一些说明
导致auto_increment变小的几种情况: 1. alter table xx auto_increment = yy; 2. truncate table 3. restart mysql 第 ...
- Visual Studio Code之常备快捷键
官方快捷键大全:https://code.visualstudio.com/docs/customization/keybindings Visual Studio Code是个牛逼的编辑器,启动非常 ...
- php-扩展模块查找下载网址
http://pecl.php.net/ 在该网页里面输入想要查找的扩展模块名 如: 搜索结果: