题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
#include <cmath> #include <cstdlib> #include <iostream> #include <math.h> #include <ostream> #include <sstream> #include <string> using namespace std; int main() { string str, real; getline(cin, str); real = str.substr(2); int length = real.length(); int max_pow = length - 1; int trans = 0, c = 0, a = 0; for (int i = 0; i <= max_pow; i++) { switch (real[i]) { case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': c = (int)real[i] - 87; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': c = (int)real[i] - 55; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': c = (int)real[i] - 48; break; default: break; } a = c * pow(16, max_pow - i); trans += a; } cout << trans; } // 64 位输出请用 printf("%lld")