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 !
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.
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
思路:进制转换再做加减,较麻烦。所以直接%x吧,X输出大写字母,x输出小写字母..
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
long long n, m, ans;
while(~scanf("%llX%llX",&n,&m))
{
ans = n + m;
if(ans < 0)
printf("-%llX\n",-ans);
else printf("%llX\n",ans);
}
return 0;
}
主要是头一次用这东西,所以记录下来吧,此时我想起了玲珑学院的一道8进制减法题,顺便贴一下,有时用这些输入输出是可以大大减少计算量的。
至于下面的8进制减法为什么要用unsigned int,是因为那道题卡数据了,用int会溢出..
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
unsigned int a,b,n,ans;
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%o%o",&a,&b);
if(a < b)
{
swap(a, b);
cout << "-";
}
ans = a - b;
printf("%o\n",ans);
}
}
return 0;
}