1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /*输入一个十进制数N,将它转换成R进制数输出。 Input 输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。 Output 为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。 Sample Input 7 2 23 12 -4 3 Sample Output 111 1B -11 */ #include<iostream> #include<cmath> int main() { using namespace std; int n,r; int a[10000]; char b[10000]; while(cin>>n>>r) { int t=0,h,i=0,j; int k=abs(n); if(r<10) { while(k!=0) { t=k%r; a[i]=t; i++; k=k/r; } if(n>0) { for(j=i-1;j>=0;j--) cout<<a[j]; cout<<endl; } if(n<0) { a[i-1]=a[i-1]*(-1); for(j=i-1;j>=0;j--) cout<<a[j]; cout<<endl; } } if(r>10) { while(k!=0) { t=k%r; if(t>9) { if(t==10) b[i]='A'; if(t==11) b[i]='B'; if(t==12) b[i]='C'; if(t==13) b[i]='D'; if(t==14) b[i]='E'; if(t==15) b[i]='F'; } else { b[i]=t+48; } i++; k=k/r; } if(n>0) { for(j=i-1;j>=0;j--) cout<<b[j]; cout<<endl; } if(n<0) { cout<<"-"; for(j=i-1;j>=0;j--) cout<<b[j]; cout<<endl; } } } } |