16进制加法

来源:互联网 发布:2017淘宝天猫商家数目 编辑:程序博客网 时间:2024/04/30 19:10

杭电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类型

1 0
原创粉丝点击