PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

时间:2022-09-06 23:56:25

PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

题目描述:

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 ∑ki=0(aibi). 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.

译: 向前或向后书写都是相同的数称为回文数 。例如 1234321 就是一个回文数。所有的单个数字的数都是回文数。

虽然回文数在十进制系统中考虑的最多,但是回文数的概念可以应用到任意自然数数字系统中。考虑一个数 N > 0 在进制 b ≥ 2 ,被写成如 ∑ki=0(aibi) 这样的标准记录。这里,通常对于所有的 i 有 0 ≤ ai < b 并且 ak 是非零的。当且仅当 对所有的 i 都有ai = ak-i 。 0 在任意进制中都写成 0 并且是一个回文数。

现在给你任意十进制整数 N 和进制 b ,你应该说明 N 是否是 b 进制下的一个回文数。


Input Specification (输入说明):

Each input file contains one test case. Each case consists of two positive 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.

译:每个输入文件包含一个测试用例,每个用例包含两个正整数 Nb 是十进制下的数字 0<N≤109 2≤b≤109。数字间用空格分隔。


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.

译:对于每个测试用例,在一行中输出,如果 Nb 数制下的回文数输出 Yes 否则输出 No。接下来的一行,用格式"ak ak -1 ... a0 " 输出 N 在 b 进制下的数字。注意 行末没有多与的空格。


Sample Input 1 (样例输入1):

27 2

Sample Output 1 (样例输出1):

Yes
1 1 0 1 1

Sample Input 2 (样例输入2):

121 5

Sample Output 2 (样例输出2):

No
4 4 1

The Idea:

涉及到回文数,首先将数字 N 转为 b 进制下的数字存入数组中,然后再根据题目意思 判断 ai = ak-i 。如果有不满足的,令标致为 false 并退出循环。最后根据 flag 的值输出 Yes or No ,再输出 b 进制下的 N .


The Codes:

#include<bits/stdc++.h>
using namespace std;
//string str = "";
int base[32] ;
int main(){
int n , b , t , cnt = 0 , flag = 1 ;
cin >> n >> b ;
while(n){
base[cnt ++] = n % b ; // 取余
n /= b ;
}
for(int i = 0 ; i < cnt ; i ++){
if(base[i] != base[cnt - i - 1]){
flag = 0 ;
break ;
}
}
printf((flag?"Yes\n":"No\n")); // 输出是否是一个回文数
for(int i = cnt - 1 ; i >= 0 ; i --)
printf("%d%c",base[i] , (i == 0 )?'\n':' ');
return 0;
}

PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642的更多相关文章

  1. PAT &lpar;Advanced Level&rpar; Practice 1019 General Palindromic Number &lpar;20 分&rpar; (进制转换,回文数)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  2. PAT &lpar;Advanced Level&rpar; Practice 1027 Colors in Mars &lpar;20 分&rpar; 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  3. PAT &lpar;Advanced Level&rpar; Practice 1011 World Cup Betting &lpar;20 分&rpar; 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

  4. PAT &lpar;Advanced Level&rpar; Practice 1005 Spell It Right &lpar;20 分&rpar; 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  5. PAT &lpar;Advanced Level&rpar; Practice 1001 A&plus;B Format &lpar;20 分&rpar; 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

  6. PAT Advanced 1019 General Palindromic Number &lpar;20 分&rpar;

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. 1019 General Palindromic Number &lpar;20 分&rpar;

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  8. PAT甲题题解-1019&period; General Palindromic Number &lpar;20&rpar;-又是水题一枚

    n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cs ...

  9. 【PAT甲级】1019 General Palindromic Number &lpar;20 分&rpar;

    题意: 输入两个正整数n和b(n<=1e9,2<=b<=1e9),分别表示数字的大小和进制大小,求在b进制下n是否是一个回文串,输出“Yes”or“No”,并将数字n在b进制下打印出 ...

随机推荐

  1. Thrift架构~目录

    回到占占推荐博客索引 概念相关 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ru ...

  2. background-position还可以这样用

    文章同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401626453&idx=1&sn=6af07 ...

  3. CentOS下httpd下php 连接mysql 本机可以,127&period;0&period;0&period;1不能访问

    你看到的这个文章来自于http://www.cnblogs.com/ayanmw php代码很简单: $server="127.0.0.1"; println("Begi ...

  4. Android ROM 备书

    1. Android ROM 目录接口 我们经常说的刷ROM是刷系统的意思,但是ROM的原意并不是这样,ROM的全称是read only memory只读储存器,正因为它是“只读”的,而且系统文件通常 ...

  5. HttpWebRequest和HttpWebResponse

    原文:http://blog.csdn.net/haitaofeiyang/article/details/18362225 申公前几日和一个客户做系统对接,以前和客户对接一般采用webservice ...

  6. lex&amp&semi;yacc3

    YACC yacc  $$  translate relation ================================================================== ...

  7. When Colon Scripting is comming&lpar;JavaScript语法扩充&rpar;

    当冒号脚本来临-- JavaScript语法扩充 连续好几夜的不能安眠,得出结论就是,未来语言未来的编译器应该是支持语法定制规则和语法扩展的,这样使得编程语言不断进化以更利于人类使用!of cours ...

  8. Delphi ADO数据操作封装类

    [delphi] view plaincopyprint? { 将数据集操作方面的东西全部封装成一个单独的类 TcustomAdoDataSet是TadoQuery.TadoTable.TadoDat ...

  9. Linux初学者的总结

    总算在年前把Linux的一些基本命令学完了,总结一下学到的(比较实用的)东西 我这里用的是Red Hat Enterprise Linux7的社区版本centos7 首先最重要的文件查看命令:ls.d ...

  10. Mabits简单应用 2017&period;8&period;3

    http://www.cnblogs.com/wushiqi54719880/archive/2011/07/26/2117601.html