输入:
0xA
0xAA
输出:
10
170
注意点
- sum
long long
防止爆int
- 字符是数字还是字母
isdigit
-
str[i] - 'A' + 10
后面加10
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string str;
while(cin >> str)
{
long long sum = 0;
for(int i = str.size() - 1, base = 1; i > 1; i --, base *= 16)
{
if(isdigit(str[i]))
sum += (str[i] - '0') * base;
else
sum += (str[i] - 'A' + 10) * base;
}
cout << sum << endl;
}
return 0;
}