题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
#include <iostream> #include <string> #include <math.h> using namespace std; int main() { string sTmp; while(cin >> sTmp) { int length = sTmp.size(); if(sTmp[0] != '0' || sTmp[1] != 'x') { break; } //int n = 0; int result = 0; for(int i = length-1; i>1; i--) { if(sTmp[i] >= '0' && sTmp[i] <= '9') { result += (sTmp[i] - '0')*pow(16, length-1-i); } else if(sTmp[i] >= 'A' && sTmp[i] <= 'F') { result += (sTmp[i] - 'A' + 10)*pow(16, length-1-i); } } cout << result <<endl; } return 0; }