题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
幂函数pow
#include <cmath> #include <iostream> #include <algorithm> using namespace std; void trans_to_ten(std::string s){ int count = 0; int len = size(s); for(int i = 0; i < len-2; i ++){ char bit = s[i+2]; if(bit >= '0' && bit <= '9') count += (bit-'0')*pow(16, len-2-i-1); else{ switch (bit) { case 'A':count += 10*pow(16, len-2-i-1);break; case 'B':count += 11*pow(16, len-2-i-1);break; case 'C':count += 12*pow(16, len-2-i-1);break; case 'D':count += 13*pow(16, len-2-i-1);break; case 'E':count += 14*pow(16, len-2-i-1);break; case 'F':count += 15*pow(16, len-2-i-1);break; default:break; } } } cout << count << endl; } int main() { std::string s; while(cin>>s){ trans_to_ten(s); } } // 64 位输出请用 printf("%lld")