A + B Again

来源:互联网 发布:淘宝模拟试衣服 编辑:程序博客网 时间:2024/05/17 04:40
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
 

题目求的是十六进制的加法。刚开始想的是把十六进制转化为十进制,进行加法运算后,再转化为十六进制。

所以这题可以直接用十六进制输入,然后进行十六进制的运算(其实不管是什么进制,在计算机中都是以二进制来计算的,只是按输入输出的格式不同,而强制转化为其它的进制),就像十进制的加法一样。

这里要注意的是输入小于15位,结果超过了二进制中的32位而小于64位。所以这里用__int64的类型。输入输入出格式就是(%I64x,%I64X)。由于%I64X,不能输出负数,所以负数的输出要做处理。

 

复制代码
 1 #include <stdio.h> 2  3 int main(){ 4     __int64 a; 5     __int64 b; 6     __int64 c; 7      8     while(scanf("%I64X%I64X",&a,&b)!=EOF){ 9         c=a+b;10         11         if(c<0){12             c=-c;13             printf("-");14         }15         printf("%I64X\n",c);16     }17         18     return 0;19 }
0 0
原创粉丝点击