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

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进制数的加法。

思路:进制转换再做加减,较麻烦。所以直接%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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值