题目:
进制转换
时间限制(普通/Java) :
1000 MS/ 3000 MS 运行内存限制 : 65536 KByte
总提交 : 1819 测试通过 : 525
题目描述
将一个十进制数N转换成R进制数输出,2≤R≤16,R≠10。
输入
多行。第一行指出以下一共有多少组数据,后续每行包含两个整数N和R,以空格分隔,-100000≤N≤100000,2≤R≤16,R≠10。
输出
多行。每行给出转换后的R进制数。
样例输入
3
7 2
23 12
-4 3
样例输出
111
1B
-11
提示
题目来源
GUOJ
题目分析:
简单题。利用itoa将十进制转换成任意进制。。
代码如下:
/*
* a.cpp
*
* Created on: 2015年3月31日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn = 32;
char numbers[maxn];
void toUpper(char str[]){
int i = 0;
while(str[i] != '\0'){
if(str[i] >= 'a' && str[i] <= 'z'){
str[i] -= 32;
}
i++;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
int base;
scanf("%d%d",&n,&base);
bool flag = false;
if(n < 0){
n = -n;
flag = true;
}
itoa(n,numbers,base);
toUpper(numbers);
if(flag == true){
printf("-");
}
printf("%s\n",numbers);
}
return 0;
}