“蓝桥杯”练习系统 - 基础练习 - 十六进制转十进制
注意long long
string存+反向迭代(其实没必要)
1 #include <iostream> 2 using namespace std; 3 typedef long long ll; 4 5 int main() 6 { 7 string s; 8 cin >> s; 9 ll ans = 0; 10 ll t = 1; 11 for (string::reverse_iterator r = s.rbegin(); r != s.rend(); r++) { 12 char x = *r; 13 ll n; 14 if (x == 'A') n = 10; 15 else if (x == 'B') n = 11; 16 else if (x == 'C') n = 12; 17 else if (x == 'D') n = 13; 18 else if (x == 'E') n = 14; 19 else if (x == 'F') n = 15; 20 else n = x - '0'; 21 ans += t * n; 22 t *= 16; 23 } 24 cout << ans << endl; 25 26 return 0; 27 }