A + B Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33030 Accepted Submission(s): 13374
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
解题心得:
刚开始做的时候蠢到写了进制转换的函数,16进制转10进制,10进制转16进制。乱得要死,后来发现使用C语言的格式控制可以直接实现进制的运算。
其中:%d 有符号32位整数
%lld 有符号64位证书
%llx有符号64位16进制整数
%u 无符号32位整数
注意输出16进制整数的时候需要使用%llX格式,这样才能够输出大写的字母。。
代码如下:
#include <iostream>
#include <string.h>
#include <cmath>
using namespace std;
int main()
{
long long a,b,result;
while(scanf("%llx%llx",&a,&b)!=EOF)
{
result = a+b;
if(result >= 0)
printf("%llX\n",result);
else
printf("-%llX\n",-result);
}
return 0;
}