(训练题第二期)A + B Again 【HDU - 2057】

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 limitMemory limitOSSource
1000 ms32768 kBWindows校庆杯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

InputOutput
+A -A0
+1A 122C
1A -911
-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;
		}
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值