- 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
#include<iostream>
using namespace std;
int main()
{ int n;
cin>>n;
int i=0;
while(i<n)
{
i++;
int N,R;
cin>>N>>R;
int index=0;
if(N<0)
{
N=-N;
index=-1;
}
int a[18];
if(!N)
{ a[0]=0;
cout<<a[0];
}
int j=0;
while(N)
{
a[j]=N%R;
N=N/R;
j++;
}
if(index<0)
cout<<'-';
for(int k=j-1;k>=0;k--)
{
if(a[k]>=10)
printf("%c",a[k]-10+'A');
else
cout<<char(a[k]+48);
}
cout<<endl;
}
return 0;
}
2097

被折叠的 条评论
为什么被折叠?



