码迷,mamicode.com
首页 > 其他好文 > 详细

A + B Again

时间:2014-11-11 18:28:42      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   sp   for   div   on   

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
 

题目求的是十六进制的加法。刚开始想的是把十六进制转化为十进制,进行加法运算后,再转化为十六进制。

所以这题可以直接用十六进制输入,然后进行十六进制的运算(其实不管是什么进制,在计算机中都是以二进制来计算的,只是按输入输出的格式不同,而强制转化为其它的进制),就像十进制的加法一样。

这里要注意的是输入小于15位,结果超过了二进制中的32位而小于64位。所以这里用__int64的类型。输入输入出格式就是(%I64x,%I64X)。由于%I64X,不能输出负数,所以负数的输出要做处理。

 

 1 #include <stdio.h>
 2 
 3 int main(){
 4     __int64 a;
 5     __int64 b;
 6     __int64 c;
 7     
 8     while(scanf("%I64X%I64X",&a,&b)!=EOF){
 9         c=a+b;
10         
11         if(c<0){
12             c=-c;
13             printf("-");
14         }
15         printf("%I64X\n",c);
16     }
17         
18     return 0;
19 }

 

A + B Again

标签:des   style   blog   io   color   sp   for   div   on   

原文地址:http://www.cnblogs.com/zqxLonely/p/4089766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!