Problem Description
Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. ^_^
Input
Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.
Output
Output A+B in decimal number in one line.
Sample Input
1 9
A B
a b
Sample Output
10
21
21
Author
威士忌
Source
HZIEE 2007 Programming Contest
康复训练第二题。
题意:计算两个十六进制数相加。hexadecimal是十六进制。
思路:很尴尬的一直在wa,后来发现,原来16进值是可以多位数的。就相当于十进制数可以输入111和222相加。所以代码就不能用char来接收a和b两个输入,因为不够啊。所以要用string。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int zhengling(char a)
{
if(a-'0'<10) return a = a-'0';
else if (a<'a') return a = a-'A'+10;
else return a = a-'a'+10;
}
int main()
{
string a,b;
while(cin >> a >> b)
{
int numa = 0,numb = 0;
for(int i = 0;i<a.length();i++)
numa=numa*16+zhengling(a[i]);
for(int i = 0;i<b.length();i++)
numb=numb*16+zhengling(b[i]);
cout<<numa+numb<<endl;
}
return 0;
}