A + B Again
题目 [HDU - 2057]
There must be many A + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal integers , your task is to calculate the sum of them,and print it in hexadecimal too.
Easy ? AC it !
翻译:我们的HDOJ中肯定存在很多A + B问题,现在又出现了一个新问题。
给你两个十六进制整数,你的任务是计算它们的总和,并以十六进制打印。
简单 ? AC吧!
Time limit | Memory limit | OS | Source |
---|---|---|---|
1000 ms | 32768 kB | Windows | 校庆杯Warm Up |
Input
The input contains several test cases, please process to the end of the file.
Each case consists of two hexadecimal integers A and B in a line seperated by a blank.
The length of A and B is less than 15.
翻译:
输入包含几个测试用例,请处理到文件末尾。
每个案例由一个由空格分隔的行中的两个十六进制整数A和B组成。
A和B的长度小于15。
Output
For each test case,print the sum of A and B in hexadecimal in one line.
翻译:对于每个测试用例,在一行中以十六进制打印A和B的总和。
Example
Input | Output |
---|---|
+A -A | 0 |
+1A 12 | 2C |
1A -9 | 11 |
-1A -12 | -2C |
1A -AA | -90 |
问题链接: [HDU - 2057]
问题描述
输入两个十六进制数,求这两个十六进制数的和,然后用输出结果(也是十六进制的结果)。
问题分析
输入的时候接受两个十六进制数 这里要注意的是输入小于15位,结果超过了二进制中的32位而小于64位,用int数据会溢出,所以用long long
直接计算和
但是如果结果是负数的话,十六进制表示负数用的是补码,先暂时转正再在前面加上“-”(负号)就好了
Sample中的结果都是大写输出的(难怪我直接输出是WA)所以加控制符,让它输出十六进制数X以大写输出。
代码
AC代码贴上来
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long long a, b, c;
while (cin >> hex >> a >> b)
{
c = a + b;
if (c >= 0)
{
cout << setiosflags(ios::uppercase) << hex << c << endl;
}
else
{
c = -c;
cout <<'-'<< setiosflags(ios::uppercase) << hex << c << endl;
}
}
}