进制转换

Description:

将一个十进制数N转换成R进制数输出,2≤R≤16,R≠10。

Input:

多行。第一行指出以下一共有多少组数据,后续每行包含两个整数N和R,以空格分隔,-100000≤N≤100000,2≤R≤16,R≠10。

Output:

多行。每行给出转换后的R进制数。

Sample Input:

3
7 2
23 12
-4 3

Sample Output:

111
1B
-11

Note:

 

本题由旧版NOJ导入,来源:GUOJ

 

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <stdio.h>
#include <iomanip>
#include <math.h>   
#include <list>
#include <tuple> 
#include <stack>
#include <string.h>
using namespace std;


string hexConversion(int num, int k) {
        // write your code here
        if(num == 0)
            return std::to_string(0);
        int tmpnum;
        if(num < 0)
            tmpnum = -num;
        else
            tmpnum = num;
        string ret;
        //cout<<tmpnum<<endl;
        while(tmpnum)
        {
            int yushu = tmpnum % k;
            int shang = tmpnum / k;
            if(yushu >= 10)
            {
                switch(yushu)
                {
                    case 10:
                        {
                            ret = 'A'+ret;
                            break;
                        }
                    case 11:
                        {
                            ret = 'B'+ret;
                            break;
                        }
                    case 12:
                        {
                            ret = 'C'+ret;
                            break;
                        }
                    case 13:
                        {
                            ret = 'D'+ret;
                            break;
                        }
                    case 14:
                        {
                            ret = 'E'+ret;
                            break;
                        }
                    case 15:
                        {
                            ret = 'F'+ret;
                            break;
                        }
                }
            }
            else
                ret = std::to_string(yushu)+ret;
            tmpnum = shang;
        }
        if(num < 0)
            ret = '-'+ret;
        return ret;
    }



int main(void)
{
	int n;
	cin >> n;
	for(;n--;)
	{
		int a, b;
		cin >> a >> b;
		cout << hexConversion(a, b) << endl;
	}
	return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值