题目

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<iostream>
using namespace std;int a[20]={0};int num=0;
void reverse(int n,int d){while(n!=0){a[num++]=n%d;n/=d;}
}int main(){int n,d;scanf("%d %d",&n,&d);reverse(n,d);int flag=0;for(int i=0;i<num/2;i++){if(a[i]!=a[num-1-i]){printf("No\n");flag=1;break;}}if(!flag)  printf("Yes\n");for(int i=num-1;i>=0;i--){printf("%d",a[i]);if(i!=0) printf(" ");}if(num==0) printf("0");return 0;
}

学习到的点是:特殊点 0的问题。有特殊点为0时是Yes,则判断时先考虑输出No的情况。判断时可使用标识flag,只要有错flag设为1,输出No,且终止所有循环判断。标识未被改变,在循环外输出Yes。

还有输出空格时的讲究,可以用i的值控制。

PAT甲级1019 回文数相关推荐

  1. PAT甲级1019 General Palindromic Number:[C++题解]进制位、回文数、vector来做

    文章目录 题目分析 题目链接 题目分析 ac代码 #include<bits/stdc++.h> using namespace std;//判回文数 bool check(vector& ...

  2. 【PAT】进制回文数

    题目描述 如果一个数字从左边读和从右边读一样,那么这个数字就是一个回文数.例如32123就是一个回文数:17在某种意义上也是一个回文数,因为它的二进制型式--10001--是一个回文数. 请你帮忙开发 ...

  3. PAT乙级(Basic Level)练习题 进制回文数

    题目描述 如果一个数字从左边读和从右边读一样,那么这个数字就是一个回文数.例如32123就是一个回文数:17在某种意义上也是一个回文数,因为它的二进制型式--10001--是一个回文数. 请你帮忙开发 ...

  4. PAT练习 进制回文数(进制问题+字符串处理)

    题目描述: 如果一个数字从左边读和从右边读一样,那么这个数字就是一个回文数.例如32123就是一个回文数:17在某种意义上也是一个回文数,因为它的二进制型式--10001--是一个回文数. 请你帮忙开 ...

  5. PAT乙级篇之进制回文数

    原题: 如果一个数字从左边读和从右边读一样,那么这个数字就是一个回文数.例如32123就是一个回文数:17在某种意义上也是一个回文数,因为它的二进制型式--10001--是一个回文数. 请你帮忙开发一 ...

  6. 【简便解法】1079 延迟的回文数 (20分)_31行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优解+分析汇总-->传送门 给定一个 k+1 位的正整数 N,写成 ak-a1a0的形式,其中对所有 i 有 0≤a​i<10 且 a​k ​​ ...

  7. leetcode 5. Longest Palindromic Substring 字符串中的最长回文数 逐步从O(n^2)优化至线性时间

    题目 解析 思路一 暴力解法 思路二 指针+最大长度 思路3 由中间至两边找回数 思路4 Manacher's algorithm 线性时间 参考文档 题目 链接 给定一个字符串 s,找到 s 中最长 ...

  8. 【力扣网练习题】回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  9. LeetCode实战:回文数

    题目英文 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

最新文章

  1. 计算机音乐情深深雨蒙蒙,情深深雨蒙蒙 MIDI File Download :: MidiShow
  2. umi脚手架搭建的项目_15天零成本搭建静态博客,托管于Github Page
  3. 计算机书籍-Go语言并发之道
  4. 【Network Security!】认识进程与端口
  5. 在React 组件中使用Echarts
  6. 【Groovy】自定义 Xml 生成器 BuilderSupport ( 构造 Xml 节点类 | 封装节点名称、节点值、节点属性、子节点 | 将封装的节点数据转为 Xml 字符串 )
  7. 服务器搬迁方案_数据中心机房改造搬迁IDC机房工程建设
  8. MongoDB升级导致启动失败
  9. 双显示器N卡安装ubuntu驱动以及解决办法
  10. (46)HTML网页开发流程
  11. 根据TTL值判断目标主机的类型
  12. objective-C 的OOP(上)--类定义、继承及方法调用
  13. 流水作业调度问题 Johnson 算法
  14. gta4android2018,gta4apk手机版
  15. 传奇GEE引擎版本如何封挂?GEE引擎设置简单的封挂脚本教程
  16. 一文搞懂移动设备ID的那些事儿
  17. linux软链接命令,linux ln 命令使用参数详解(ln -s 软链接)
  18. XML第二讲:格式正规的XML文档、XML文档的元素、属性、实体深度详解
  19. win10无法登陆到你的账户
  20. Android多媒体之SoundPool+pcm流的音频操作

热门文章

  1. js小学生图区_js实现图片区域可点击大小随意改变(适用移动端)代码实例
  2. mysql5.5java安装_配置非安装版的mysql 5.5
  3. layui如何获取父节点的父节点_layui树形组件(右键、父节点选中子节点全被选中)...
  4. Python yaml模块
  5. HTML十进制字符编号
  6. zabbix PHP databases support off Fail
  7. 《Core Java 课件》Day02
  8. 华为机试HJ8:合并表记录
  9. java发送jsp表格邮件_JSP 发送邮件
  10. java职称考试试题_SSM框架下的JAVA职称考试模拟系统