zoukankan      html  css  js  c++  java
  • A + B Again

    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
    ",c);
    16     }
    17         
    18     return 0;
    19 }
  • 相关阅读:
    Jquery 面板导航,切换效果
    Jquery闪耀的地方,dom遍历,过滤查找的例子
    Jquery 通过 data- 来实现按钮点击切换显示隐藏
    Bootstrap 点击按钮切换内容
    c语言快速学习
    嵌入式学习
    16_文件的操作
    06_指针
    11_函数的退出方式
    10_参数数量可变的函数及命令行参数
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/4089766.html
Copyright © 2011-2022 走看看