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。

最新文章

  1. JavaScript Array map() 方法
  2. Log4net源码View之Logger解析
  3. Samba出现“您可能没有权限使用网络资源”解决方法
  4. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
  5. centos 6.5下安装文件上传下载服务
  6. Redis-key的设计技巧
  7. ASP.NET Core 处理 404 Not Found
  8. oracle导不出空表的解决办法
  9. SAP MM 明明已经扩展供应商到采购组织下,采购订单里还是报错?
  10. 数字音频处理的瑞士军刀sox的音效算法以及用法
  11. HDU--1540 Tunnel Warfare(线段树区间更新)
  12. h5 右下角浮动按钮, 纯css实现
  13. percona-toolki安装冲突(my.cnf Percona-Server-shared与mysql-community-server)
  14. PHP在linux读取word文档
  15. Javascript的闭包及其使用技巧实例
  16. [Unity3D]Unity3D游戏开发之角色控制漫谈
  17. Android -- onMeasure
  18. iOS 常用的几个math函数
  19. Action(8):Error-26608:HTTP Status-Code=504(Gateway Time-out)
  20. C++ string的用法

热门文章

  1. J-query extend()方法
  2. Azure Load Balancer : 简介
  3. vue项目环境搭建
  4. 饿了么element UI&lt;el-dialog&gt;弹出层&lt;/el-dialog&gt;修改默认样式不能在&lt;style scoped&gt;修改
  5. 领跑衫获奖感言 &amp; 课程总结
  6. 2-Twentieth Scrum Meeting-20151220
  7. 【读书笔记】Linux内核设计与实现(第三章)
  8. Linux内核分析作业第四周
  9. Linux课题实践四——ELF文件格式分析
  10. 第三个Sprint冲刺第四天(燃尽图)