bit Number Converter


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值