HDU2057A + B Again

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


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


Author
linle


开始以为系统没法接受16进制数据,自己写了16进制转换

#include <iostream>
#include <string.h>
using namespace std;

const int MAX = 30;

char a[MAX], b[MAX];

int d[6] = {10, 11, 12, 13, 14, 15};
char h[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
char Hex[MAX];
int atoi(char c)
{
	return c - '0';
}
char atoc(int n)
{
	return '0' + n;
}
_int64 pow(int x, int y)
{
	int i;
	_int64 result = 1;
	for(i = 1; i <= y; i++)
		result *= x;
	return result;
}
//获取十进制
_int64 GetDec(int len, char c[])
{
	int i, j;
	_int64 sum;
	for(i = len - 1, j = 0, sum = 0; i >= 0; i--, j++)
	{
		if(c[i] == '-')
		{
			sum *= -1;
			continue;
		}else if(c[i] == '+')
			continue;
		if(isdigit(c[i]))
			sum += atoi(c[i]) * pow(16, j);
		else
		{
			sum += d[c[i] - 'A'] * pow(16, j);
		}
	}
	return sum;
}

void GetHex(_int64 num)
{
	int i = 0, temp, j;
	if(!num)
	{
		cout<<'0'<<endl;
		return;
	}
	if(num < 0)
	{
		cout<<'-';
		num *= -1;
	}
	while(num)
	{
		temp = num % 16;
		if(temp > 9)
			Hex[i++] = h[temp - 10];
		else
			Hex[i++] = atoc(temp);
		num /= 16;
	}
	for(j = i - 1; j >= 0; j--)
		cout<<Hex[j];
	cout<<endl;
}
int main()
{
	int alen, blen;
	while(cin >> a >> b)
	{
		alen = strlen(a);
		blen = strlen(b);
		GetHex(GetDec(alen, a) + GetDec(blen, b));
	}
	return 0;
}


简介版:
# include <stdio.h>


int main ()
{
    _int64 a, b, sum ;
    while (~scanf ("%I64X%I64X", &a, &b))
    {
        sum = a + b ;
        if (sum < 0)
        {
            printf ("-") ;
            sum *= -1 ;
        }
        printf ("%I64X\n", sum) ;
    }
    return 0 ;
}

你可能感兴趣的:(HDU)