HDU 2057 A + B Again 【16进制加法】

来源:互联网 发布:淘宝首页焦点图在哪里 编辑:程序博客网 时间:2024/04/29 20:34

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 121A -9-1A -121A -AA
 

Sample Output
02C11-2C-90
 
题意:计算两个16进制数的加法。

思路:进制转换再做加减,较麻烦。所以直接%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;}


0 0
原创粉丝点击