问题描述
Problem Description
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 !
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.
Output
For each test case,print the sum of A and B in hexadecimal in one line.
Sample Input
+A -A
+1A 12
1A -9
-1A -12
1A -AA
Sample Output
0
2C
11
-2C
-90
问题分析
Problem Analyse
在HDOJ上已经有很多道A+B的题目了,现在有来了道新的。
给你两个16进制数,你的任务就是计算它们的和,然后以16进制输出。简单吗?把它AC掉!
C编程基础知识
Algorithm Analyse
本题的数据量不大,用64位整数不会溢出。
用%I64X就可以对数据读入输出进行操作。但问题在于直接用%I64X,是不能输出负数的,所以我们碰到负数要自己转成正数,在前面加‘-’。
算法实现
判断是否为负数,可以直接判断是否<0,也可以求二进制最高位上是不是为1:(a+b)&((__int64)1<<63)
参考源码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main(void) | |
{ | |
__int64 a, b; | |
while(scanf("%I64x%I64X", &a, &b) != EOF) | |
printf(a+b<0?"-%I64X\n":"%I64X\n", a+b<0?-a-b:a+b); | |
return 0; | |
} |