hdu 2057A + B Again(十六进制或者八进制的A+B)

A + B Again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14001    Accepted Submission(s): 6129


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
 
题目大意:进行十六进制的加减法
解题思路:
这是一个非常有意思的题,关键是怎么样进行十六进制的转化。这里有一些小技巧。
%X 或者是%x,可以直接输出十六进制的数字(大写的X,则对应大写的输出,例如A、B、.....。同理,小写的x对应小写的输出)
还有几种格式,分别对应几种输出:%I64X,%llX相当于十进制中的%I64d,%lld。(八进制的也同理)
下面看代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
char s1[20],s2[20];
LL a,b;
char s[20];
int main()
{
    while(~scanf("%s%s",s1,s2))
    {
        sscanf(s1,"%I64X",&a);
        sscanf(s2,"%I64X",&b);
        LL c=a+b;
        sprintf(s,"%I64d",c);
        if(s[0]=='-')
        {
            printf("-");
        }
        c=fabs(c);
        printf("%I64X\n",c);
    }
    return 0;
}

还有一个:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
LL a,b;
LL ans;
int main()
{
    while(~scanf("%I64X%I64X",&a,&b))
    {
       ans=a+b;
       if(ans<0)
       {
           ans=-1*ans;
           printf("-%I64X\n",ans);
       }
       else printf("%I64X\n",ans);
    }
    return 0;
}


你可能感兴趣的:(hdu 2057A + B Again(十六进制或者八进制的A+B))