PAT-进制转换-A1019 General Palindromic Number (20分)

题目描述:

  给出两个整数n、b,问十进制整数n在b进制下是否是回文数,若是,则输出Yes;否则,输出No。在此之后输出n在b进制下的表示。

  单词:Palindromic Number--回文数;decimal--十进制的。

输入格式:

  输入用空格分隔的两个数,第一个数是十进制数n(0<N≤10?9),第二个数是十进制数b( 2≤b≤10?9)。

输出格式:

  第一行输出Yes或No,第二行输出十进制数n在b进制下的表示,用空格把每个数隔开,末尾不能有多余空格。

样例:

  样例一:

    输入:27 2

    输出:第一行:Yes     第二行:1 1 0 1 1

  样例二:

    输入:121 5

    输出:第一行:No      第二行:4 4 1

思路:

  1. 先将十进制数n转换为b进制数
  2. 对数进行判断,由数组,比较位置i和num-i-1的数,只要有一个数不同,就不是回文数。

注意点:

  注意特殊数据,0的输出。

代码:

 1 #include<iostream>
 2 using namespace std;
 3
 4 //存储转换后的数的每一位数
 5 int datas[48] = { 0 };
 6
 7 int main()
 8 {
 9     int n, b, num = 0;
10     bool flag = true;
11
12     scanf("%d %d", &n, &b);
13
14     //进行进制转换
15     do
16     {
17         datas[num++] = n % b;
18         n /= b;
19     } while (n != 0);
20
21     //对转换后的数进行判断
22     for (int i = 0;i < num/ 2;i++)
23     {
24         if (datas[i] != datas[num - i - 1])
25         {
26             flag = false;
27             break;
28         }
29     }
30
31     //输出判断结果
32     if (flag)printf("Yes\n");
33     else printf("No\n");
34
35     //输出转换结果
36     for (int i = num - 1;i >= 0;i--)
37     {
38         printf("%d", datas[i]);
39         if (i != 0)printf(" ");
40     }
41
42     return 0;
43 }

参考书籍-《算法笔记》-胡凡

原文地址:https://www.cnblogs.com/fangzhiyou/p/12404651.html

时间: 2024-10-08 02:13:00

PAT-进制转换-A1019 General Palindromic Number (20分)的相关文章

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 Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Although palindromic numbers are most often cons

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

PAT甲题题解-1019. General Palindromic Number (20)-又是水题一枚

n转化为b进制的格式,问你该格式是否为回文数字(即正着写和倒着写一样)输出Yes或者No并且输出该格式又是水题... #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=30; int a[maxn]; int n,b; int main() { scanf("%d %d&q

PAT/进制转换习题集

B1022. D进制的A+B (20) Description: 输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数. Input: 输入在一行中依次给出3个整数A.B和D. Output: 输出A+B的D进制数. Sample Input: 123 456 8 Sample Output: 1103 1 #include <cstdio> 2 3 int main() 4 { 5 int a, b, d; 6 scanf(&q

PAT A1019 General Palindromic Number

PAT A1019 General Palindromic Number 题目描述: 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. Altho

枚举 + 进制转换 --- hdu 4937 Lucky Number

Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s): 49 Problem Description “Ladies and Gentlemen, It’s show time! ” “A thief is a creative artist who ta

pat1019. General Palindromic Number (20)

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

1019 General Palindromic Number (20)(20 point(s))

problem 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 of

PAT甲级——A1019 General Palindromic Number

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 cons