题解 | #进制转换#
进制转换
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() { // int a, b; // while (cin >> a >> b) { // 注意 while 处理多个 case // cout << a + b << endl; // } string str, real; getline(cin, str); real = str.substr(2); // cout<<real<< endl; int length = real.length(); int max_pow = length - 1; int trans = 0, c = 0, a = 0; for (int i = 0; i <= max_pow; i++) { // cout<<real[i]*1<< endl; // cout<<max_pow<< endl; // cout<<pow(16, max_pow-i)<< endl; // cout<<real[i]<< endl; // cout<<(int)real[i]<< endl; // cout<<c<< endl; // cout<<real[i]<< endl; switch (real[i]) { case 'a': case 'A': c = 10; break; case 'b': case 'B': c = 11; break; case 'c': case 'C': c = 12; break; case 'd': case 'D': c = 13; break; case 'e': case 'E': c = 14; break; case 'f': case 'F': c = 15; 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; } // cout << "c is " << c << endl; // cout << "pow is " << max_pow - i << endl; // cout << "pow is " << pow(16, max_pow - i) << endl; a = c * pow(16, max_pow - i); // cout << "a is " << a << endl; trans += a; // cout << trans << endl; // cout<<trans<< endl; } cout << trans; } // 64 位输出请用 printf("%lld")