joj1006

 1006: All your base


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 5967 1984 Standard

Given a base, and two positive integers in decimal (base 10), convert the two numbers to the new base, add them, and display their sum in the new base.

Input

Three positive integers denoting the base and the two numbers, respectively. Input numbers will be integers between 0 and 65535. Bases will be between 2 and 10 inclusive. Each case will be on a separate line. The end of input will be denoted by three zeros.

Output

An equation for the sum of the two numbers, in the new base.

Example

In this example, we add 10 and 3 in base 2, and we add 15 and 4 in base 3.

In base 2, 10 = 8 + 2 = 1*23 + 0*22 + 1*21 + 0*20 and 3 = 2 + 1 = 1*21 + 1*20, so their base 2 equivalents are 1010 and 11, respectively. 10 + 3 = 13 = 1*23+1*22+0*21+1*20, so the base 2 equivalent of 13 is 1101.

In base 3, 15 = 9 + 6 = 1*32+2*31+0*30 and 4 = 3 + 1 = 1*31 + 1*30, so their base 2 equivalents are 120 and 11, respectively. 15 + 4 = 19 = 2*32 + 0*31 + 1*30, so the base 3 equivalent of 19 is 201.

Input

2 10 3
3 15 4
0 0 0

Output

1010 + 11 = 1101
120 + 11 = 201
 
进制转换,水题一枚。
 1 #include <stdio.h>
2
3 void change(int a, int n);
4
5 int main(void)
6 {
7 int n, a, b, sum;
8
9 while (scanf("%d%d%d", &n, &a, &b), n!=0 && a!=0 && b!=0)
10 {
11 sum = a + b;
12
13 change(a, n);
14 printf(" + ");
15 change(b, n);
16 printf(" = ");
17 change(sum, n);
18 printf("\n");
19 }
20
21 return 0;
22 }
23
24 void change(int a, int n)//a换成n进制并输出
25 {
26 int t[20] = {0};
27
28 int i = 0;
29
30 while (a != 0)
31 {
32 t[i++] = a % n;
33 a /= n;
34 }
35
36 for (int p=0, q=i-1; p<q; ++p, --q)
37 {
38 int m = t[p];
39 t[p] = t[q];
40 t[q] = m;
41 }
42 for (int p=0; p<i; p++)
43 {
44 printf("%d", t[p]);
45 }
46 }

 


This problem is used for contest: 53  167  181  184 


Submit / Problem List / Status / Discuss

posted @ 2012-01-27 21:59  漂木  阅读(160)  评论(0编辑  收藏  举报