题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
int main() {
// 获取实际十六进制的数据
string str;
getline(cin, str);
str = str.substr(2, str.size() - 2);
// 创建最终结果与位标识
int res = 0, count = 0;
// 由于字符串是高位在前低位在后,故反向遍历
for (int i = str.size() - 1; i >= 0; i--) {
// 如果是字母 A的ASCII码为65 但A在十六进制中表示10
if (isalpha(str[i])) {
res += ((int)str[i] - 55) * pow(16, count++);
// 如果是数字 0的ASCII码48
} else {
res += ((int)str[i] - 48) * pow(16, count++);
}
}
cout<<res<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
#机试#