铂金组第三题

2018-12-13  本文已影响0人  李耳_9992

A + B Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37781 Accepted Submission(s): 15312

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

问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2057
问题简述:输入两个十六进制的数,以十六进制输出它们的和
问题分析:注意以16进制输入并以16进制输出,字母大写,16进制数为负时,以补码形式输出。
程序分析:当C<0时,C=-C,输出‘-’和C;

#include<iomanip>
setiosflags(ios::uppercase)//输出大写字母
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    long long A, B,C;
    while (cin >> hex >> A >> B)
    {
        C = A + B;
        if (C < 0)
        {
            C = -C;
            cout <<"-"<< setiosflags(ios::uppercase)<<hex <<C ;
        }
        else
            cout << setiosflags(ios::uppercase) <<hex<< C;
        cout << endl;
    }
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读