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 a​i​​ as ∑​i=0​k​​(a​i​​b​i​​). Here, as usual, 0≤a​i​​<b for all i and a​k​​ is non-zero. Then N is palindromic if and only if a​i​​=a​k−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 "a​k​​ a​k−1​​ ... a​0​​". 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

鸣谢网友“CCPC拿不到牌不改名”修正数据!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
#include <set>
#include <map>
using namespace std;
const int maxn = ;
int s[maxn] = { }, s2[maxn] = { };
int main() {
int n, b;
cin >> n >> b; if (n == ) {
printf("Yes\n0");
return ;
}
int i = ;
while (n != ) {
s[i] = n % b;
s2[i] = s[i];
n /= b;
i++;
}
reverse(s, s+i);
int flag = ;
for (flag; flag < i; flag++) {
if (s[flag] != s2[flag]) {
break;
}
}
if (flag == i) printf("Yes\n");
else printf("No\n"); int j;
for (j = ; j < i-; j++) {
printf("%d ", s[j]);
}
printf("%d", s[i-]);
system("pause");
}

注意点:不能用string来做,因为b可能会很大,用string无法存储太大的数字,最多就255,还是要开一个int数组。一开始觉得用string超容易,只要存进来然后reverse一下直接==比较就ok了,发现有两个测试点一直过不了,难。

PAT A1019 General Palindromic Number (20 分)——回文,进制转换的更多相关文章

  1. PAT A1019 General Palindromic Number (20 分)

    AC代码 #include <cstdio> const int max_n = 1000; long long ans[max_n]; int num = 0; void change( ...

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

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  3. PAT (Advanced Level) Practice 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 ...

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

  5. 【PAT甲级】1019 General Palindromic Number (20 分)

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

  6. [PAT] A1019 General Palindromic Number

    [题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...

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

  8. PAT 1019 General Palindromic Number[简单]

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

  9. pat1019. General Palindromic Number (20)

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

随机推荐

  1. Hybris IMPEX.

    1.Impex是基于java Model的一种面向对象的数据操作手段,因此写impex代码前需要理清java Model之间的依赖关系.   2.基本语法:mode type[modifier=val ...

  2. CSS属性disabled和readonly的区别是什么

    在博客园中看到这样一篇文章,关于disabled和readonly的区别,以前还真的没有注意它们的区别,还是有必要知道它们的区别的,所以转载了. 这两个属性有类似之处,但是区别也是巨大的,之所以说类似 ...

  3. 【读书笔记】iOS-UI Automation 需要遵守的规则

    1,被测试的应用程序必须是Developer签名的应用程序或者是运行在模拟器里面的应用程序. 2,在被测试的应用程序开发的过程中需要处理UI控件的可访问性.使用IB的开发工程师需要在XIB中加入一个A ...

  4. struts2文件上传大小限制问题小结(引用)

    最后解决办法: 页面js控制上传文件的大小,在页面进行控制.如下代码 inputs是所有文本上传input DOM //名称信息 var nameStr=''; //大小信息 var sizeStr= ...

  5. C# 6 元组应用 Part 1:方便的字典工厂方法

    首先是简单的实现: public static class CollectionExtensions { public static IDictionary<TKey, TValue> M ...

  6. Lightning框架示例 - 动态建立Lightning组件

    动态建立Lightning组件 组件化前端开发是Lightning框架的优点之一.在进行Lightning应用开发时,我们可以将组件进行嵌套.引用,从而实现模块的封装和重用,提高开发效率. 组件的嵌套 ...

  7. SpringMVC处理请求

    HttpServletBean HttpServletBean主要参与了创建工作,并没有涉及请求的处理. FrameworkServlet FrameworkServlet的service方法里添加了 ...

  8. Appium学习——Appium工作原理

    appium的工具原理 Appium-client>>>>Appium-server>>>>移动设备 ========================= ...

  9. C#重试公用类

    //Retry机制 public static class RetryExecutor { /// <summary> /// 重试零个参数无返回值的方法 /// </summary ...

  10. csdn中使用git的一些注意事项---免得走弯路

    csdn中使用git必须的条件(windows系统下): 1.本机当前登录用户文件夹下必须有.ssh隐藏文件,并且这个文件中必须有用git bash中用命令生成的密钥文件:id_rsa  id_rsa ...