PAT/进制转换习题集

Posted 爱CY真是太好了

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("%d%d%d", &a, &b, &d);
 7 
 8     int sum = a+b;
 9     int ans[31], num = 0;
10     do {
11         ans[num++] = sum%d;
12         sum /= d;
13     } while(sum != 0);
14 
15     for(int i=num-1; i>=0; --i)
16         printf("%d", ans[i]);
17 
18     return 0;
19 }

 

A1019. General Palindromic Number (20)

Description:

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:

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:

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 Input1:

27 2

Sample Output1:

Yes
1 1 0 1 1

Sample Input2:

121 5

Sample Output2:

No

4 4 1

 1 #include <cstdio>
 2 
 3 #define MaxSize 50
 4 int ans[MaxSize];
 5 
 6 int main()
 7 {
 8     //freopen("E:\\Temp\\input.txt", "r", stdin);
 9 
10     int N, b, num = 0;
11     bool flag = true;
12     scanf("%d %d", &N, &b);
13 
14     do {
15         ans[num++] = N%b;
16         N /= b;
17     } while(N != 0);
18 
19     for(int i=0, j=num-1; i<=(num-1)/2, j>=(num-1)/2; ++i, --j) {
20         if(ans[i] != ans[j]) {
21             flag = false;
22             break;
23         }
24     }
25 
26     if(flag == true)    printf("Yes\n");
27     else printf("No\n");
28     for(int i=num-1; i>=0; --i) {
29         printf("%d", ans[i]);
30         if(i != 0)  printf(" ");
31     }
32 
33     return 0;
34 }
 1 #include <cstdio>
 2 
 3 bool judge(int z[], int num)
 4 {
 5     for(int i=0; i<=num/2; ++i) {
 6         if(z[i] != z[num-1-i])  return false;
 7         else return true;
 8     }
 9 }
10 
11 int main()
12 {
13     int n, b, z[40], num = 0;
14     scanf("%d%d", &n, &b);
15 
16     do {
17         z[num++] = n%b;
18         n /= b;
19     } while(n != 0);
20     bool flag = judge(z, num);
21 
22     if(flag == true)    printf("Yes\n");
23     else printf("No\n");
24     for(int i=num-1; i>=0; --i) {
25         printf("%d", z[i]);
26         if(i != 0)  printf(" ");
27     }
28 
29     return 0;
30 }

 

以上是关于PAT/进制转换习题集的主要内容,如果未能解决你的问题,请参考以下文章

Python练习题2.8转换函数使用

PAT 1027 Colors in Mars (20分)

PAT-1015 Reversible Primes (20 分) 进制转换+质数

PAT 1010 Radix 进制转换+二分法

PAT练习题目录

PAT-进制转换-B1022 D进制的A+B (20分)