Posted on 2008-06-23 18:39
superman 阅读(515)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
1 /* Accepted 1334 C++ 00:00.00 840K */
2 #include <iostream>
3
4 using namespace std;
5
6 char c[] = "0123456789ABCDEF";
7
8 void convert(string & num, int s, int t)
9 {
10 if(s == t)
11 {
12 printf("%7s\n", num.c_str());
13 return;
14 }
15
16 int n = 0;
17 for(int i = 0; i < num.size(); i++)
18 {
19 int m = 1;
20 for(int k = 1; k < num.size() - i; k++)
21 m *= s;
22 if(num[i] >= 'A' && num[i] <= 'F')
23 n += (num[i] - 'A' + 10) * m;
24 else
25 n += (num[i] - '0') * m;
26 }
27
28 string str;
29 while(n)
30 {
31 str += c[n % t];
32 n /= t;
33 }
34
35 if(str.size() > 7)
36 printf("%7s\n", "ERROR");
37 else
38 {
39 for(int i = 0; i < str.size() / 2; i++)
40 swap(str[i], str[str.size() - i - 1]);
41 printf("%7s\n", str.c_str());
42 }
43 return;
44
45 cout << endl;
46 }
47
48 int main()
49 {
50 string num;
51 int s, t;
52
53 while(cin >> num >> s >> t)
54 convert(num, s, t);
55
56 return 0;
57 }
58