立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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 a​i​​ as ∑​i=0​k​​ (a​i​​ b​i​​ ). Here, as usual, 0≤a​i​​ <b for all i and a​k​​ is non-zero. Then N is palindromic if and only if a​i​​ =a​k−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<N≤10 ^​9​​ is the decimal number and 2≤b≤10 ^​9​​ 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 "a​k​​ a​k−1​​ … a​0​​ ". 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


题意: 给一个十进制数a,进制b。 要求将a转化为b进制,判断转化后的数是否为回文数。

while+除法+取余求进制数。存入vector后翻转,比较翻转前后是否相等。


#include<bits/stdc++.h>
using namespace std;
int main() {int a, b; cin >> a >> b;vector<int>v1, v2;        //存储翻转前和翻转后的最终结果while(a) {v1.push_back(a%b);a /= b;}v2 = v1; reverse(v1.begin(), v1.end());cout << (v1==v2 ? "Yes" : "No") << '\n';for(int i = 0; i < v1.size(); i++) {if(i != 0) putchar(' ');cout << v1[i];} return 0;
}

耗时:

1019 General Palindromic Number (20分)_18行代码AC相关推荐

  1. 【题意分析】1024 Palindromic Number (25 分)_38行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...

  2. 【PAT甲级 BigInteger】1019 General Palindromic Number (20 分) Java版 7/7通过

    题目 一开始只使用了Long,有后面四个测试点过不去,后来换了BigInteger,就通过了. 这题用Java的BigInteger做,可以操作任意长度的数字,感觉有一点取巧了. 如果C或者C++的话 ...

  3. 【PAT甲级 进制转换】1019 General Palindromic Number (20 分) Java版 7/7通过

    题目 这道题可以说是非常友善了,说白了是个水题.题目没什么坑,一次通过,主要思想就是: 输入两个数:num和base 将num按照base进制转换,得到arr 判断arr是否是一个回文数,并且输出这个 ...

  4. 【题意+解析】1041 Be Unique (20 分)_18行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Being unique is so important to people on Mars that even their lo ...

  5. 浙大PAT甲级1019. General Palindromic Number (20)

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  6. 1019. General Palindromic Number (20)

    题目连接:https://www.patest.cn/contests/pat-a-practise/101 原题如下: A number that will be the same when it ...

  7. 1081 Rational Sum (20 分)_22行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given N rational numbers in the form numerator/denominator, you a ...

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

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

  9. 1094 谷歌的招聘 (20分)_25行代码AC

    立志用最少的代码做更高效的表达 PAT乙级最优题解-->传送门 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com ...

最新文章

  1. Ueditor 自动换行,添加P标签问题
  2. python类的函数_python 类函数
  3. win服务器管理器“丢失”了怎么办?
  4. B2B 企业如何高效获客增长?
  5. 【牛客 - 272C】Balls(概率dp,结论,思维)
  6. oracle中的(+)
  7. Mysql的IF()函数:如果返回True值,返回第一个值,为False值,返回第二个值
  8. 《深入体验 飞鸽传书 开发内幕 核心基础》
  9. java包含_【Java】判断字符串是否包含子字符串
  10. iforums之UEditor上传图片提示【未知错误】
  11. android之日志库logger
  12. 微信语音怎么转发?微信转发语音居然是这样做的…
  13. 国内外无代码可视化开发平台一览
  14. Android RecyclerView只显示第一行
  15. sparkstreaming的实时黑名单过滤太慢
  16. 计算k段流水线执行n条指令的执行时间
  17. QT 可视化界面设计
  18. ASCLL字母表顺序
  19. 在VS2103环境中集成Doxygen工具
  20. iview upload组件手动控制上传,excel表格导入

热门文章

  1. 数据结构与算法 | 用栈实现队列
  2. TCP协议疑难杂症全景解析|硬核
  3. 面试题:1 到 1000 之间有多少个 7?
  4. IntelliJ IDEA 2019从入门到癫狂 图文教程
  5. Linux AV1硬件视频解码将支持Intel Tiger Lake
  6. 音视频技术开发周刊 | 158
  7. 不需要SFU实现WebRTC联播实践
  8. 数据结构与算法之希尔排序
  9. AI商业产品经理:我眼中的AI简史
  10. FFMPEG结构体分析之AVCodec