今天做了一道蓝桥杯的练习题,我把num和h定义为一个很长的整数,当我用long定义的时候竟然也不成,经过搜索资料换了无符号整数,结果就比较令人满意喽。
/*
#include<iostream>
#include<string>
using namespace std;
int main()
{
string hex;
cin >> hex;
int len = hex.length();
unsigned long num = 0;
unsigned long h = 1;
for(int i = len-1; i >= 0;i--){
int c = 0;
switch(hex[i])
{
case ‘0‘:c=hex[i]-‘0‘;break;
case ‘1‘:c=hex[i]-‘0‘;break;
case ‘2‘:c=hex[i]-‘0‘;break;
case ‘3‘:c=hex[i]-‘0‘;break;
case ‘4‘:c=hex[i]-‘0‘;break;
case ‘5‘:c=hex[i]-‘0‘;break;
case ‘6‘:c=hex[i]-‘0‘;break;
case ‘7‘:c=hex[i]-‘0‘;break;
case ‘8‘:c=hex[i]-‘0‘;break;
case ‘9‘:c=hex[i]-‘0‘;break;
case ‘A‘:c=hex[i]-‘A‘+10;break;
case ‘B‘:c=hex[i]-‘A‘+10;break;
case ‘C‘:c=hex[i]-‘A‘+10;break;
case ‘D‘:c=hex[i]-‘A‘+10;break;
case ‘E‘:c=hex[i]-‘A‘+10;break;
case ‘F‘:c=hex[i]-‘A‘+10;break;
default :break;
}
num += c * h;
h = h * 16;
}
cout << num << endl;
return 0;
}
原文:http://www.cnblogs.com/HuangAQiu/p/6363701.html