杭电2031进制转换

http://acm.hdu.edu.cn/showproblem.php?pid=2031

这个题主要是注意判断大于R大于10的情况下进行处理,还有负号不能忘了处理,提前输出就行,其余的与以前做过的并没什么不同了,差不多还是以前的思路

 1 #include <cstdio>
 2 #include<cstring>
 3 #include <cstdlib>
 4 #include<iostream>
 5 using namespace std ;
 6 const int MAXN = 1010 ;
 7 int main()
 8 {
 9     int R,N;
10     while(cin>>N>>R)
11     {
12         if (N == 0)
13             cout<<'0' ;
14             int i = 0 ;
15             int a[MAXN] ;
16         while(N > 0)
17         {
18             a[i++] = N%R ;
19             N = N/R ;
20         }
21         if( N < 0)
22         {
23             printf("-") ;
24             N = -N ;
25             while(N>0)
26             {
27                 a[i++] = N%R ;
28                 N = N/R ;
29             }
30         }
31         for(int j = i-1 ; j >= 0 ; j--)
32         {
33             if (a[j] <= 9)
34                 printf("%d",a[j]);
35             else
36             {
37                 switch(a[j])
38                 {
39                 case 10:
40                     printf("A");
41                     break;
42                 case 11:
43                     printf("B");
44                     break;
45                 case 12:
46                     printf("C");
47                     break;
48                 case 13:
49                     printf("D");
50                     break;
51                 case 14:
52                     printf("E");
53                     break;
54                 case 15:
55                     printf("F");
56                     break;
57                 }
58             }
59         }
60         printf("
");
61     }
62     return 0 ;
63 }
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3145282.html