华为oj 005

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25244495/article/details/82183605

【华为OJ】【005-进制转换】

题目链接:

https://blog.csdn.net/DERRANTCM/article/details/51249114

C++实现:

int HexToInt(char ch)
{
	if (ch >= '0' && ch <= '9')
	{
		return ch - '0';
	} 
	else if(ch >= 'a' && ch <= 'f')
	{
		return ch - 'a' + 10;
	}
	else if(ch >= 'A' && ch <= 'F')
	{
		return ch - 'A' + 10;
	}
	else return 0;
}

int GetDecimal(string num)
{
	int sum =0;
	for (int i=2;i<num.size();i++)
	{
		sum = sum * 16 + HexToInt(num.at(i));
	}
	return sum;
}

void main()
{
	string str1;
	vector<string> v1;
	
	do 
	{
		getline(cin,str1);
		if(!str1.empty())
			v1.push_back(str1);
	} while (!str1.empty());

	for (int i=0;i<v1.size();i++)
	{
		cout << GetDecimal(v1[i]) << endl;
	}
	system("pause");

猜你喜欢

转载自blog.csdn.net/qq_25244495/article/details/82183605
005