UVa 389 Basically Speaking

又一道基础题,进制转换。

做水题也不能大意!

  
    
1 // 68ms 2010-05-14 15:48:58
2   // log: wa(1) ac(1)
3 // Type: Base number conversion(进制转换)
4 // key1: strrev() 在ANSI C, C++ 中不能使用
5 // key2: 0的时候要处理
6 // Thinking: After finished coding, member to try some special data.
7 #include < stdio.h >
8 #include < string .h >
9 #define NL 100
10
11 int get ( char a)
12 {
13 if (a >= ' 0 ' && a <= ' 9 ' ) return a - ' 0 ' ;
14 return a - ' A ' + 10 ;
15 }
16
17 int main()
18 {
19 char s0[NL], s1[NL];
20 int len, I, K, M, b1, b2;
21 while (scanf( " %s%d%d " , s0, & b1, & b2) != EOF) {
22 len = strlen(s0);
23 int t = 0 , bb = 1 ;
24 I = len - 1 ;
25 while (I >= 0 ) {
26 t += get (s0[I]) * bb;
27 bb *= b1;
28 I -- ;
29 }
30 K = 0 ;
31 while (t > 0 ) {
32 I = t % b2;
33 if (I >= 10 ) s1[K ++ ] = I - 10 + ' A ' ;
34 else s1[K ++ ] = I + ' 0 ' ;
35 t /= b2;
36 }
37 if (K == 0 ) { s1[K ++ ] = ' 0 ' ; }
38 s1[K] = ' \0 ' ;
39 if (K > 7 ) puts( " ERROR " );
40 else {
41 for (I = 0 , M = K - 1 ; M >= 0 ; M -- ) {
42 s0[I ++ ] = s1[M];
43 }
44 s0[I] = ' \0 ' ;
45 printf( " %7s\n " , s0);
46 }
47 }
48 return 0 ;
49 }
50 /*
51 input:
52 0000000 2 2
53 */

 

你可能感兴趣的:(call)