Posted on 2009-07-29 12:39
Hero 阅读(114)
评论(0) 编辑 收藏 引用 所属分类:
代码如诗--ACM
1 //HLOJ 1015 Accepted 15 136 833 C++
2
3 #include <iostream>
4 using namespace std ;
5
6 int tnum ;
7 int inn, ink ;
8
9 void fitoa( int val, char str[], int r )
10 {
11 if( 0 == val )
12 {
13 str[0] = '0' ;
14 str[1] = '\0' ;
15 }
16 else
17 {
18 char strtemp[100] ; int cnt1 = 0 ;
19
20 while( val > 0 )
21 {
22 strtemp[cnt1++] = (char)(val % r + '0') ;
23 val = val / r ;
24 }
25
26 int cnt2 = 0 ;
27 while( cnt1 >0 )
28 {
29 str[cnt2++] = strtemp[--cnt1] ;
30 }
31 str[cnt2] = '\0' ;
32 }
33 }
34
35 int main()
36 {
37 while( cin >> tnum )
38 {
39 while( tnum -- )
40 {
41 cin >> inn >> ink ;
42
43 bool isNegative = false ;
44 if( inn < 0 ) isNegative = true ;
45
46 int inm = inn > 0 ? inn : -inn ;
47
48 char out[100] ;
49 fitoa( inm, out, ink ) ;
50
51 printf( "%d ", inn ) ;
52 if( inn < 0 ) printf( "-%s\n", out ) ;
53 else printf( "%s\n", out ) ;
54 }
55 }
56 return 0 ;
57 }