Number Converter
时间限制: 1秒 内存限制: 64M
Problem Description
As we know 3 = 11 if we think about the binary representation . Now let’s make a number converter. Given you two integers n (0 <= n <=10^9) and k (2 <= k <= 36), please output the representation of the n in the radix specified by k. And in the representation, you should use A, B, … to represent 10, 11, …
Input
This problem contains multiple test cases.
For each test case, there are two integers n and k.
Output
For each test case, please output the representation of the n in the radix specified by k.
Sample Input
3 2
4 3
15 16
Sample Output
11
11
F
题目大意:将输入的数n (0 <= n <=10^9) 转化为 k (2 <= k <= 36)进制
#include<stdio.h>
void convert(int n,int k,char *result,char *radix,int *length){
//将n转化为k进制,并将结果存到result中,转换完后的结果的长度存到length指到的位置;
int i = 0;
result[i] = radix[n%k];
n /= k;
++i;
for(;n;++i){
result[i] = radix[n%k];
n /= k;
}
*length = i - 1;
return ;
}
void display(char *result,int length){
//将结果显示出来
for(;~length;--length){
printf("%c",result[length]);
}
printf("\n");
return ;
}
int main(){
//将可能用到的基数存到一个数组中
char radix[37]; //存放基数
char result[100]; //转化为K进制后的结果
int n;//输入的n
int k;//要转化的K进制
int length;// 转换后的结果的长度
char ch;
int i;
ch = 'A' - 10;
for(i = 0; i < 10;++i){
radix[i] = '0' +i;
}
for(i = 10; i < 36; ++i){
radix[i] = ch + i;
}
radix[36] = 0;
while(~scanf("%d%d",&n,&k)){
convert(n,k,result,radix,&length);
display(result,length);
}
return 0;
}