您的位置:首页 > 其它

1252进制转换

2017-08-20 14:38 197 查看
进制转换

Problem Description

输入一个十进制数N,将它转换成R进制数输出。

Input

输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R != 10)。

Output

为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

Example Input

7 2
23 12
-4 3


Example Output

111
1B
-11


#include<iostream>
#include<stack>
using namespace std;
int main()
{
int n,r;
while(cin>>n>>r)
{
stack<char>S;
if(n==0)
{
cout<<"0"<<endl;//0的任何进制都是0
continue;
}else if(n<0)
{
cout<<"-";//如果是负数,提前输出符号,并对其绝对值进行处理,同正数
n*=-1;
}
int t;
while(n)
{
t=n%r;
if(t<10)
{
t+='0';
S.push(t);
}
else
{
t=t+'A'-10;
S.push(t);
}
n/=r;
}
while(!S.empty())
{
cout<<S.top();
S.pop();
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: