H106OJ | 进制转换
题目
分析
成功案例:
按题目说明输出(八进制后无空格)
十六 | 十 | 八 |
---|---|---|
000 | 000 | 000 |
10F | 271 | 417 |
A5E | 2654 | 5136 |
8B2 | 2226 | 4262 |
BCD | 3021 | 5715 |
失败案例:
八进制后有空格、换行都不行
十六 | 十 | 八 |
---|---|---|
111 | 273 | 421 |
FFF | 4095 | 7777 |
FFF | 4095 | 7777 |
2FC | 764 | 1374 |
123 | 291 | 443 |
代码
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
using namespace std;
int AtoInt(string s,int radix)
{
int sum = 0;
for(int i=0; i<s.size() ;i++)
{
char word = s[i];
if(word >= '0' && word <= '9')
sum += pow(radix,(s.size()-i-1))*(word-'0');
else if(word >= 'a' && word <= 'f')
sum += pow(radix,(s.size()-i-1))*(word-'a'+10);
else
sum += pow(radix,(s.size()-i-1))*(word-'A'+10);
}
return sum;
}
int main()
{
string s ;
cin >> s;
int result = AtoInt(s,16);
cout << setiosflags(ios::uppercase) << hex << result <<" ";
cout << dec << result <<" ";
cout << oct << result;
return 0;
}