PAT/进制转换习题集

2018-06-18 04:13:01来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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 }

 

A1027. Colors in Mars (20)

Description:

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output:

For each test case you should output the Mars RGB value in the following format: first output "#", then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a "0" to the left.

Sample Input:

15 43 71

Sample Output:

#123456

 1 #include <cstdio>
 2 
 3 char radix[13] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'};
 4 
 5 int main()
 6 {
 7     int r, g, b;
 8     scanf("%d%d%d", &r, &g, &b);
 9 
10     printf("#");
11     printf("%c%c", radix[r/13], radix[r%13]);
12     printf("%c%c", radix[g/13], radix[g%13]);
13     printf("%c%c", radix[b/13], radix[b%13]);
14 
15     return 0;
16 }

 

A1058. A+B in Hogwarts (20)

Description:

If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- as Hagrid explained it to Harry, "Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it's easy enough." Your job is to write a program to compute A+B where A and B are given in the standard form of "Galleon.Sickle.Knut" (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

Input:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.

Output:

For each test case you should output the sum of A and B in one line, with the same format as the input.

Sample Input:

3.2.1 10.16.27

Sample Output:

14.1.28

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     int a[3], b[3], c[3];
 6     scanf("%d.%d.%d %d.%d.%d", &a[0], &a[1], &a[2], &b[0], &b[1], &b[2]);
 7 
 8     int carry = 0;
 9     c[2] = (a[2]+b[2])%29;
10     carry = (a[2]+b[2])/29;
11     c[1] = (a[1]+b[1]+carry)%17;
12     carry = (a[1]+b[1]+carry)/17;
13     c[0] = a[0]+b[0]+carry;
14 
15     printf("%d.%d.%d", c[0], c[1], c[2]);
16 
17     return 0;
18 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C语言可以开发哪些项目?

下一篇:2048小游戏4X4C语言