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. C#之概述

    当前流行的开发语言概述 C#是微软公司为Visual Studio开发平台推出的一种简洁.类型安全的面向对象的编程语言,开发人员可以通过她编写在.NET Framework上运行的各种安全可靠的应用程 ...

  2. Cocos2D v2.0至v3.x简洁转换指南(四)

    实现通用的update方法 在Cocos2d 2.x你需要2个步骤去实现在每帧调用update方法: // 1) schedule update [self scheduleUpdate]; ... ...

  3. SpriteBuilder给节点添加effect在32设备上发生crash

    环境为 Xcode 6.4 , cocos2D 3.0.4 , SpriteBuilder 1.4.9 在给某一节点添加Effect后,运行在真机iphone4s上发生崩溃,显示为: 可以看到整个堆栈 ...

  4. 菜鸟玩云计算之廿二: saltstack 配置

    菜鸟玩云计算之廿二: saltstack 配置 要求环境: RHEL6.4+ >=Python2.6.6, < Python 3.0 关闭salt-master/minion服务:   # ...

  5. 【Matlab编程】Matlab及Java小时钟

    一年前曾经用matlab的gui做了一个时钟,由于是直接用GUIDE和ActiveX控件写的,程序虽说有许多行,大多数都是自动生成的,自己写的只有十几行而已.闲着没事,就耗费了下午的时间用matlab ...

  6. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  7. 打印机威胁:嵌入式Web服务有安全问题

    现在大多数打印机.扫描仪,以及VoIP系统等设备都会内建嵌入式的Web服务,这主要是为了方便管理.然而不幸的是,这些设备大多会由于设置问题而处在无保护状态下.有些服务甚至可以使用默认的帐号和密码访问, ...

  8. 利用可变参实现fprintf函数

    #include <stdio.h> #include <stdarg.h> /* 可变参相关接口 typedef char * va_list ; void va_start ...

  9. 采用UltraISO制作U盘启动盘

    采用UltraISO制作U盘启动盘 打开UltralSO,选择"文件"--->"打开",如下图: 图1 打开WIN7操作系统的ISO文件,如下图: 图2 ...

  10. java的finalize方法使用

    1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法. finalize()与C++中的析构函数 ...