首页 > 代码库 > 【美团~牛客】十六进制转十进制

【美团~牛客】十六进制转十进制

技术分享
 1 // 16_10jinzhi.cpp : 定义控制台应用程序的入口点。 2 // 3  4 #include "stdafx.h" 5 #include<iostream> 6 #include<string> 7 #include<math.h> 8 using namespace std; 9 10 11 int getNum(char c)12 {13     int temp = 0;14     switch(c)15     {16     case A:17         temp = 10;18         break;19     case B:20         temp = 11;21         break;22     case C:23         temp = 12;24         break;25     case D:26         temp = 13;27         break;28     case E:29         temp = 14;30         break;31     case F:32         temp = 15;33         break;34     case 0:35         temp = 0;36         break;37     case 1:38         temp = 1;39         break;40     case 2:41         temp = 2;42         break;43     case 3:44         temp = 3;45         break;46     case 4:47         temp = 4;48         break;49     case 5:50         temp = 5;51         break;52     case 6:53         temp = 6;54         break;55     case 7:56         temp = 7;57         break;58     case 8:59         temp = 8;60         break;61     case 9:62         temp = 9;63         break;64     }65     return temp;66 }67 68 int _tmain(int argc, _TCHAR* argv[])69 {70     string OX_num;71     while(cin>>OX_num)72     {73         int len =  OX_num.length();74         if(len <= 2)75             return -1;76         int result = 0;77         for(int i = 2; i < len;i++)78         {79             int temp = 0;80             if((OX_num[i]>=A && OX_num[i] <= F)||    (OX_num[i]>=0 && OX_num[i] <= 9) )81                 temp = getNum(OX_num[i]);82             else83                 return -1;84 85             result += temp*(pow(static_cast<double>(16),len - i - 1));86         }87         cout<<result<<endl;88     }89     return 0;90 }
View Code

 

【美团~牛客】十六进制转十进制