进制转换

Posted by Cww97 on 2016-03-14

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/50887848

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<cstdio>
using namespace std;
char b[100];
int i,k,x,a[10000];
int main (){
for (i=0;i<=9;i++)b[i]=(char)(i+'0');
for (i=10;i<=36;i++)b[i]=(char)(i+55);
while (scanf("%d%d",&k,&x)==2){
bool fu=false;
if (k==0){printf("0\n");continue;}
if (k<0){fu=true;k=-k;}
int top=0;
while(k){
top++;
a[top]=k%x;
k=k/x;
}
if (fu)printf("-");
for (i=top;i>=1;i--)printf("%c",b[a[i]]);
printf("\n");
}
return 0;
}