进制转换
Time Limit: 1000MS
Memory Limit: 65536KB
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
Hint
Author
HDOJ
PS:注意十六进制的表示方法,当数字%进制余出值 大于10时,10用A表示,11用B表示.....15用F表示,十六进一。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N,R;
while(cin>>N>>R)
{
stack<char>u;
if(N == 0)//0的任何进制都是0
{
cout<<"0"<<endl;
continue;
}
else if(N < 0)
{
cout<<"-";//注意判断正负
N = -N;
}
int t = 1;
while(N != 0)
{
t = N % R;
if(t < 10)
{
t = t + 48;//char类型,所以需要加上48
u.push(t);
}
else
{
t= t+55;//同上
u.push(t);
}
N = N/R;
}
int k = u.size();
for(int i = 0; i < k; i++)
{
cout<<u.top();
u.pop();
}
cout<<endl;
}
return 0;
}