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 ;
}
分享到:
相关推荐
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。 Input 本题目包含多组测试数据,请处理到文件结束。 每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。 Output 请在一行里面...
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
acm hdu as easy as a+b
2、new做两件事,一是分配内存,二是调用类的构造函数 3、new建立的是一个对象,而malloc分配的是一块内存 4、new/delete是保留字,不需要头文
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
acm入门之枚举搜索,学校第一次acm培训,包括枚举及其优化,dfs和bfs
ACM题库,一些题目和答案,以及解题报告,传上来共享
杭电OnlineJudge 200-2099的解题报告
hdu 1695 GCD(欧拉函数+容斥原理).docx
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
acm入门训练和日常训练 对于初学者以及acm爱好者有叫大帮助
HDU的一题........HDU DP动态规
hdu 3341(ac自动机+状态压缩) 题意:容易理解... 思路:首先一开始容易想到要用到dp,开设一个dp[41][41][41][41][501]的数组来解决,但是明显内存已经超出范围了,于是就想如何减少内存呢?只要知道A、T、C、G其中...
hdu2101AC代码