PAT A1019 General Palindromic Number

news/2025/2/15 23:31:12/

1019 General Palindromic Number

分数 20

作者 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 ∑i=0k​(ai​bi). 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.

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.

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

给定一个十进制数n,和一个进制d,将n转换为d进制,判断转换后的d进制串是否是一个回文串。

/*** 给定一个十进制数n,和一个进制d,将n转换为d进制,判断转换后的d进制串是否是一个回文串。
*/#include <iostream>
#include <algorithm>
#include <vector>using namespace std;vector<int> int_str(int n, int d)
{vector<int> vec;while(n){int mod = n % d;n /= d;vec.push_back(mod);}reverse(vec.begin(), vec.end());return vec;
}bool is_level(vector<int> vec)
{int len = vec.size();for(int i=0; i<len/2; ++i)if(vec[i] != vec[len-1-i])return false;return true;
}int main()
{int n, d;cin >> n >> d;vector<int> vec = int_str(n, d);if(is_level(vec))puts("Yes");elseputs("No");bool flag = true;for(auto ele : vec){if(flag)    flag = false;else cout << ' ';cout << ele;}puts("");return 0;
}


http://www.ppmy.cn/news/56554.html

相关文章

TCP/IP学习总结

目录 问题一&#xff1a;TCP/IP为什么采用分层的结构 问题二&#xff1a;为什么说传输层提供端到端的服务&#xff0c;网络层是点到点的服务 问题三&#xff1a;怎么确定IP分片中的第一片和分片需要注意的地方 问题四&#xff1a;ARP协议的理解 问题五&#xff1a;路由表中…

KVM虚拟机的定时逐次开机设定方法-systemd方式

前言&#xff1a;KVM虚拟机系统是榨取服务器最大性能的一种好方法&#xff0c;但是谁也不想让服务器启动的时候要等待5分钟甚至以上的时间。 为了减轻服务器开机时的任务数量&#xff0c;需要虚拟机在开机后一段时间内&#xff0c;按顺序逐次启动&#xff0c;以达到各个服务间的…

spring security regexrequestmatcher 认证绕过漏洞(CVE-2022-22978)

漏洞详情 CVE-2022-22978 中&#xff0c;在Spring Security受影响版本范围内&#xff0c;若使用了存在特殊配置&#xff08;含 .&#xff09;的RegexRequestMatcher 的情况下可能导致权限绕过。 受影响版本&#xff1a; 5.5.0 < Spring Security < 5.5.7 5.6.0 < …

辨析 可交付成果、核实的可交付成果、验收的可交付成果、最终成果

可交付成果、核实的可交付成果、验收的可交付成果、最终成果 辨析 可交付成果 指的是在某一过程、阶段或项目完成时&#xff0c;产出的任何独特并可核实的产品、成果或服务。可交付成果可能是有形的&#xff0c;也可能是无形的。 指导与管理项目工作产生可交付成果 核实的可…

CSDN周赛第48期

不知不觉又过去两期周赛&#xff0c;相应地&#xff0c;题解也落下了。而当我再回去想下载考试报告时。。。 现在更新的速度有这么快了么&#xff1f; 可惜题目还是考过的旧题&#xff0c;尤其对我们这种老油子来说&#xff0c;最大的好处是省去了阅读理解的烦恼。 平心而论&…

Unity利用UGUI RawImage组件制作转场动画

Unity利用UGUI RawImage组件制作转场动画 最近接到了一个unity全景图的小项目&#xff0c;由于在不同的场景之间转场时直接转会太过生硬&#xff0c;因此要求有个Alpha转场的动画。于是想到两种可行的方案&#xff1a; 一、UGUI方案 用UGUI显示当前屏幕纹理&#xff0c;然后…

【计算机图形学基础教程】MFC基本绘图函数1

MFC基本绘图函数 在Windows平台上&#xff0c;应用程序的图形设备接口&#xff08;Graphics Device Interface, GDI&#xff09;被抽象为设备上下文CDC类&#xff08;Device Context, DC&#xff09;。因此&#xff0c;直接接受图形数据信息的不是显示器和打印机等硬件设备&am…

【Linux】进程信号 --- 信号产生 信号递达和阻塞 信号捕捉

&#x1f34e;作者&#xff1a;阿润菜菜 &#x1f4d6;专栏&#xff1a;Linux系统编程 文章目录 一、预备知识二、信号产生1. 通过终端按键产生信号1.1 signal()1.2 core dump标志位、核心存储文件 2.通过系统调用向进程发送信号3.由软件条件产生信号3.1 alarm函数和SIGALRM信号…