A + B Again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31658 Accepted Submission(s): 12860
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 !
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.
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
Source
问题链接:HDU2057 A + B Again。
问题分析:
投机取巧,按照许多人的做法来做,程序是AC了。即便如此,也是一脸困惑,不知道解了这种题意义何在?因为定义变量的类型是__int64,十分的不满意。还是希望用long long定义变量,但是输入输出格式。
看一下下表,可以知道__int64 与long long 的区别。
变量定义 | 输出方式 | gcc(mingw32) | g++(mingw32) | gcc(linux i386) | g++(linux i386) | MicrosoftVisual C++ 6.0 |
---|---|---|---|---|---|---|
long long | “%lld” | 错误 | 错误 | 正确 | 正确 | 无法编译 |
long long | “%I64d” | 正确 | 正确 | 错误 | 错误 | 无法编译 |
__int64 | “lld” | 错误 | 错误 | 无法编译 | 无法编译 | 错误 |
__int64 | “%I64d” | 正确 | 正确 | 无法编译 | 无法编译 | 正确 |
long long | cout | 非C++ | 正确 | 非C++ | 正确 | 无法编译 |
__int64 | cout | 非C++ | 正确 | 非C++ | 无法编译 | 无法编译 |
long long | printint64() | 正确 | 正确 | 正确 | 正确 | 无法编译 |
计算机语言如果不标准,累死程序员。C99标准已经有了long long类型,使用该类型才是正解。
从批判中学习,付出高昂学费得到的是教训,不是正确的途径,是在浪费时间。
后来,经过一番研究,终于搞定了正解程序。那里需要注意的是,输出变量的时候,要用“%llX”,不要用“%llx”。这二者之间有微妙的差异。
程序说明:
AC的C语言程序如下(正解):
/* HDU2057 A + B Again */
#include <stdio.h>
int main(void)
{
long long a, b, result;
while(scanf("%llx %llx", &a, &b) != EOF) {
result = a + b;
if (result >= 0)
printf("%llX\n", result);
else
printf("-%llX\n", -result);
}
return 0;
}
另外一个AC的C语言程序如下(不满意的程序):
/* HDU2057 A + B Again */
#include <stdio.h>
int main(void)
{
__int64 a, b, result;
while(scanf("%I64X %I64X", &a, &b) != EOF) {
result = a + b;
if (result >= 0)
printf("%I64X\n", result);
else
printf("-%I64X\n", -result);
}
return 0;
}