1019 General Palindromic Number (20分)_18行代码AC
立志用最少的代码做最高效的表达
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 ai as ∑i=0k (ai bi ). 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 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 "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 1Sample 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相关推荐
- 【题意分析】1024 Palindromic Number (25 分)_38行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A number that will be the same when it is written forwards or bac ...
- 【PAT甲级 BigInteger】1019 General Palindromic Number (20 分) Java版 7/7通过
题目 一开始只使用了Long,有后面四个测试点过不去,后来换了BigInteger,就通过了. 这题用Java的BigInteger做,可以操作任意长度的数字,感觉有一点取巧了. 如果C或者C++的话 ...
- 【PAT甲级 进制转换】1019 General Palindromic Number (20 分) Java版 7/7通过
题目 这道题可以说是非常友善了,说白了是个水题.题目没什么坑,一次通过,主要思想就是: 输入两个数:num和base 将num按照base进制转换,得到arr 判断arr是否是一个回文数,并且输出这个 ...
- 【题意+解析】1041 Be Unique (20 分)_18行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Being unique is so important to people on Mars that even their lo ...
- 浙大PAT甲级1019. General Palindromic Number (20)
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 1019. General Palindromic Number (20)
题目连接:https://www.patest.cn/contests/pat-a-practise/101 原题如下: A number that will be the same when it ...
- 1081 Rational Sum (20 分)_22行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given N rational numbers in the form numerator/denominator, you a ...
- 【简便解法】1079 延迟的回文数 (20分)_31行代码AC
立志用更少的代码做更高效的表达 PAT乙级最优解+分析汇总-->传送门 给定一个 k+1 位的正整数 N,写成 ak-a1a0的形式,其中对所有 i 有 0≤ai<10 且 ak ...
- 1094 谷歌的招聘 (20分)_25行代码AC
立志用最少的代码做更高效的表达 PAT乙级最优题解-->传送门 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com ...
最新文章
- Ueditor 自动换行,添加P标签问题
- python类的函数_python 类函数
- win服务器管理器“丢失”了怎么办?
- B2B 企业如何高效获客增长?
- 【牛客 - 272C】Balls(概率dp,结论,思维)
- oracle中的(+)
- Mysql的IF()函数:如果返回True值,返回第一个值,为False值,返回第二个值
- 《深入体验 飞鸽传书 开发内幕 核心基础》
- java包含_【Java】判断字符串是否包含子字符串
- iforums之UEditor上传图片提示【未知错误】
- android之日志库logger
- 微信语音怎么转发?微信转发语音居然是这样做的…
- 国内外无代码可视化开发平台一览
- Android RecyclerView只显示第一行
- sparkstreaming的实时黑名单过滤太慢
- 计算k段流水线执行n条指令的执行时间
- QT 可视化界面设计
- ASCLL字母表顺序
- 在VS2103环境中集成Doxygen工具
- iview upload组件手动控制上传,excel表格导入