数的进制
题目描述
数在生活中有各种各样的表示法。时间的小时是24进制,时间的分是60进制,计算机内的数用二进制,我们平时用十进制。请你把一个十进制的数转成八进制和十六进制。
输入
输入有若干行,每行一个整数代表一个十进制数。数字范围在int以内。
输出
按要求的格式输出对应的八进制数和十六进制数。
如输入10时,输出格式是:10 = 12(8) = a(16)
输入负数时,只需要将其相反数转化成对应进制,输出时在前面添加负号即可。
样例输入 Copy
10
-2
99
100
-100
样例输出 Copy
10 = 12(8) = a(16)
-2 = -2(8) = -2(16)
99 = 143(8) = 63(16)
100 = 144(8) = 64(16)
-100 = -144(8) = -64(16)
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
using namespace std;
int mi(int n){
int x=1;
for(int i=0;i<n;i++){
x=x*16;
}
return x;
}
int main() {
char a[16] = {'0','1','2','3','4','5','6','7','8','9'
,'a','b','c','d','e','f'
};
char x[1000];
int n,eight;
string sixteen,six;
int len,res,k,e,ei,s;
bool minus,ze;
while(scanf("%d",&n)!=EOF){
ei=0;
eight=0;
sixteen="";
minus =false;
ze=false;
if(n<0){
s=abs(n);
e=abs(n);
minus=true;
}
else{
s=n;
e=n;
}
if(e%8==0){
e--;
ze=true;
}
while(e!=0){
ei = ei*10+e%8;
e=e/8;
}
//cout<<ei<<endl;
e=ei;
while(e!=0){
eight = eight*10 + e%10;
e = e/10;
}
if(ze){
eight++;
}
//cout<<eight<<endl;
//16进制
while(s!=0){
sixteen = sixteen + a[s%16];
s = s/16;
}
len = sixteen.size();
six="";
while(len>0){
six += sixteen[len-1];
len--;
}
//cout<<sixteen<<' '<<six<<' '<<endl;
string s1="aaa",s2="bbb";
//cout<<s1+s2<<endl;
if(six=="") six="0";
if(!minus){
cout<<n<<" = "<<eight<<"(8) = "<<six<<"(16)"<<endl;
}
else{
cout<<n<<" = -"<<eight<<"(8) = -"<<six<<"(16)"<<endl;
}
}
return 0;
}