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