zoukankan      html  css  js  c++  java
  • 杭电oj A + B Again

    A + B Again

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 15358    Accepted Submission(s): 6700


    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
     

    Source
    思路:
    这道题中午看了,当时打算吧十六进制转化成十进制,然后十进制转化成十六进制,但是发现这样做有困难。。。所以不会了,看了网上的代码,才明白,计算机自己就会执行十六进制的运算,只不过的不能直接输出负数,需要自己转化。。。题上说的A 和B 的长度不超过15,因为A和B 是十六进制数,如果长度不考虑正负号的话,一个数字表示4位二进制数,所以A 、B的位数是60位,所以定义A、B时,要定义为64位。当然如果考虑正负号的话,A、B含有的的数字最多为14个,这时,A、B的位数为56位,当然还要定义成64位的。。然后,把A和B相加,当然还是十六进制的数,赋值给B,判断B是不是负数,如果是负的,需变成正的,输出时,添个负号即可。。。
    代码:
    #include<stdio.h>
    int main()
    {
        __int64  A,B;
        while(scanf("%I64X %I64X",&A,&B)!=EOF)
        {
            B=A+B;
            if(B>=0)
                printf("%I64X
    ",B);
            if(B<0)
            {
                B=-B;
                printf("-%I64X
    ",B);
            }
        }
        return 0;
    }
    
    //如果不相信,认为是负数可以直接输出来的话,请看:



  • 相关阅读:
    Android开发(三十二)——延时
    Android开发(三十一)——重复引用包错误Conversion to Dalvik format failed
    Android开发(二十九)——layout_weight的含义
    Android开发(二十八)——基础功能函数
    Android开发(二十七)——android:layout_weight的真实含义
    Android开发(二十六)——Application
    Android开发(二十五)——Android上传文件至七牛
    ansible
    H3C 交换机VRRP和堆叠
    Centos 7.x 双网卡绑定
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236795.html
Copyright © 2011-2022 走看看