16进制加法

杭电2057A + BAgain

Problem Description

There must be manyA + B problems in our HDOJ , now a new one is coming.
Give you two hexadecimal(16进制) integers , your task is to calculate the sum ofthem,and print it in hexadecimal too.
Easy ? AC it !

Input

The input containsseveral 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 ablank.
The length of A and B is less than 15.

Output

For each testcase,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

1;先说题目意思,是十六进制的两个数相加求和的题目;最初看起来是比较简单的;不就是定义16进制的变量输入然后相加嘛;然而打出来后会发现怎么跟测试数据不同

-1A -12

FFFFFFFFFFFFFFD4

1A -AA

FFFFFFFFFFFFFF70

是不是很蒙的感觉,是不是与我们所想的不同;因为16进制的负数加减法与我们所理解的是10进制不同;并且输出16进制的负数不会在其前面加负号,而是变成2进制来反码补码来做;但这题目不是这样的,题目需要我们输出负号,因此需要我们在前面输出负号而原来的负数变成正数;

详情看代码;

#include<stdio.h>

#include<stdlib.h>

int main()

{

    long long int a, b;

    while(scanf("%llX %llX",&a, &b) !=EOF){

        if(a+b < 0){

            printf("-%llX\n",(-a-b));

        }

        else    printf("%llX\n",a+b);

        

    }

   

    return 0 ;

}

还需要注意一点就是需要定义long long int类型

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值