杭电oj A + B Again

时间:2021-07-23 09:05:19

A + B Again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15358    Accepted Submission(s): 6700

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
 
Author
linle
 
Source
思路:
这道题中午看了,当时打算吧十六进制转化成十进制,然后十进制转化成十六进制,但是发现这样做有困难。。。所以不会了,看了网上的代码,才明白,计算机自己就会执行十六进制的运算,只不过的不能直接输出负数,需要自己转化。。。题上说的A 和B 的长度不超过15,因为A和B 是十六进制数,如果长度不考虑正负号的话,一个数字表示4位二进制数,所以A 、B的位数是60位,所以定义A、B时,要定义为64位。当然如果考虑正负号的话,A、B含有的的数字最多为14个,这时,A、B的位数为56位,当然还要定义成64位的。。然后,把A和B相加,当然还是十六进制的数,赋值给B,判断B是不是负数,如果是负的,需变成正的,输出时,添个负号即可。。。
代码:
#include<stdio.h>
int main()
{
__int64 A,B;
while(scanf("%I64X %I64X",&A,&B)!=EOF)
{
B=A+B;
if(B>=0)
printf("%I64X\n",B);
if(B<0)
{
B=-B;
printf("-%I64X\n",B);
}
}
return 0;
}

//如果不相信,认为是负数可以直接输出来的话,请看:

杭电oj A + B Again
杭电oj A + B Again