PAT 甲级 1019 General Palindromic Number(简单题)
1019. General Palindromic Number (20)
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>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
int n,b;
int a[10005];
int cnt;
void dfs(int n,int b)
{
if(n<b)
{
a[cnt++]=n;
return;
}
dfs(n/b,b);
a[cnt++]=n%b;
}
int main()
{
while(scanf("%d%d",&n,&b)!=EOF)
{
cnt=0;
dfs(n,b);
int i=0,j=cnt-1;
int ans=0;
while(i<=j)
{
if(a[i]!=a[j])
{
ans=-1;
break;
}
i++,j--;
}
if(ans==-1)
printf("No\n");
else
printf("Yes\n");
for(int i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",a[i]);
else
printf("%d ",a[i]);
} }
return 0; }
PAT 甲级 1019 General Palindromic Number(简单题)的更多相关文章
- PAT 甲级 1019 General Palindromic Number(20)(测试点分析)
1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...
- PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- PAT甲级——1019 General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT 甲级 1019 General Palindromic Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 A number that will be ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- PAT Advanced 1019 General Palindromic Number (20 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT甲级——A1019 General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT 1019 General Palindromic Number
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
随机推荐
- VS2012安装git
新安装了vs2012,尝试使用Git,安装过程比较简单,记录下来以备以后查阅. 1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http ...
- FIFO、LRU、OPT页面调度算法及样例
网上非常多介绍3种页面置换算法的样例和过程是不对的, 本文依据<操作系统概念>第七版对三种算法做介绍,并给出正确的样例以验证算法. 一.FIFO先进先出页面置换算法,创建一个FIFO队列来 ...
- 1px 下划线solid的问题
1 物理像素线,也就是普通屏幕下 1px,高清屏幕下 0.5px的情况,采用transform属性 scale 实现即可. .mod_grid { position: relative; &: ...
- Atitit.软件开发的几大规则,法则,与原则p821.doc
Atitit.软件开发的几大规则,法则,与原则p821.doc 1. 设计模式六大原则2 1.1. 设计模式六大原则(1):单一职责原则2 1.2. 设计模式六大原则(2):里氏替换原则2 1.3. ...
- [gj]来公司停电了-维护机器
停电出现的问题 一早来机房跳闸了. 很多vm手动起来后有些服务不太正常了. 机房锁坏了.不知道谁进去干啥了.导致跳闸 闸门不知道在哪里.导致所有机器停电了 牵扯到的技术点: 1,nexus启动. ng ...
- Visual Studio/Eclipse调用 JBoss5中的WebService
1. HelloWebService.java package com.xx.webservices; import javax.jws.WebMethod; import javax.jws.Web ...
- Python 集合常用方法总结
数据类型:int/str/bool/list/dict/tuple/float/set (set类型天生去重) 一.集合的定义 s = set() #定义空集合 s = {'a','b','c' ...
- Linux之手动设置IP地址
一.引言 有时候我们装好了机器却发现网络没有设置,而服务器的机器一般都是没有界面的,那么该如何设置IP呢? 二.步骤 root用户,#vi /etc/sysconfig/network-scripts ...
- HTML5坦克大战(2)绘制坦克复习
html代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- nginx调用php-fpm出错解决方法和nginx配置详解
装完了nginx和php-5.5,配置好了nginx调用php后,就开始启动php-fpm. 使用下面的命令 复制代码 代码如下: /usr/local/php/sbin/php-fpm 就可以启动了 ...