1019. General Palindromic Number (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路

进制转换题,检验一个十进制数字用b进制数表示的时候是否是一个回文数。用一个数组保存b进制每一位的数字,然后检查是否对称就行。

代码

#include<iostream>
#include<vector>
using namespace std;
vector<int> digits(30);
int main()
{
int N,b;
while(cin >> N >> b)
{
int index = 0;
while( N != 0)
{
digits[index++] = N % b;
N = N/b;
}
bool isPal = true;
for(int i = 0;i < index/2;i++)
{
if(digits[i] != digits[index - i - 1])
{
isPal = false;
break;
}
}
if(isPal)
cout << "Yes" <<endl;
else
cout << "No" << endl;
for(int i = index - 1;i >= 0;i--)
{
cout << digits[i];
if( i != 0)
cout << " ";
}
if(index == 0 )
cout << 0;
cout << endl;
}
}

  

PAT1019:General Palindromic Number的更多相关文章

  1. pat1019. General Palindromic Number (20)

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

  2. PAT 1019 General Palindromic Number

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  3. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  4. PAT 1019 General Palindromic Number[简单]

    1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...

  5. 1019 General Palindromic Number (20 分)

    1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...

  6. PTA (Advanced Level) 1019 General Palindromic Number

    General Palindromic Number A number that will be the same when it is written forwards or backwards i ...

  7. PAT 甲级 1019 General Palindromic Number(简单题)

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

  8. PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  9. 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 ...

随机推荐

  1. (NO.00001)iOS游戏SpeedBoy Lite成形记(二)

    打开SpriteBuilder生成的Xcode项目文件,首先在MainScene添加play回调函数: -(void)play{ CCScene *gameScene = [CCBReader loa ...

  2. UML之构件图

    构件图,英文名曰:Component Diagrams用于描述一组构件之间的组织和依赖关系,用于建模系统的静态实现视图,构件可以是可执行程序.库.表.文件和文档等,她包含了逻辑类或者逻辑类的实现信息, ...

  3. MySQL内存调优

    原文链接: MySQL Memory Allocation -- by Rick James原文日期: Created 2010; Refreshed Oct, 2012, Jan, 2014 翻译人 ...

  4. 安卓TV开发(八) 移动智能终端多媒体爬虫技术 获取加载网页视频源

    转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40049137,作者:skay 从上一篇学习中,学习了多媒体技术中的怎么去用josu ...

  5. vs 2010调用matlab dll显示窗口核心代码

    matlab代码: figure('NumberTitle','off','menubar','none','toolbar','none','name','Topo Image'); x=0:pi/ ...

  6. MaterialDesign学习项目

    概述 该项目主要用来学习Material Design Support Library和一些android其他技术,也借鉴了网上一些其他优秀的学习资源.该项目目前主要分为俩大部分(后期可能会有一些增加 ...

  7. 使用javascript中读取Xml文件做成的一个二级联动菜单

    [html] view plaincopy <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...

  8. Web系统测试Web安全性测试

    WEB安全性测试介绍WEB安全性测试--拒绝服务攻击WEB安全性测试--文件上传漏洞WEB安全性测试--跨站攻击WEB安全性测试--SQL注入一WEB安全性测试--SQL注入二WEB安全性测试--SQ ...

  9. Javascript二(函数详解)

    一.函数            Javascript是一门基于对象的脚本语言,代码复用的单位是函数,但它的函数比结构化程序设计语言的函数功能更丰富.JavaScript语言中的函数是"一等公 ...

  10. EF Core使用SQL调用返回其他类型的查询

    假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法是编写 ADO.NET 代码,并从 EF 获取数据库 ...