首页 > 其他 > 详细

16进制A+B

时间:2014-02-09 17:14:39      阅读:463      评论:0      收藏:0      [点我收藏+]
Problem Description
Many classmates said to me that A+B is must needs. If you can’t AC this problem, you would invite me for night meal. ^_^
 
Input
Input may contain multiple test cases. Each case contains A and B in one line. A, B are hexadecimal number. Input terminates by EOF.
 
Output
Output A+B in decimal number in one line.
 
Sample Input
1 9
A B
a b
 
Sample Output
10
21
21

 注:输入十六进制,需要转化

bubuko.com,布布扣
#include<iostream>
using namespace std;

int htod(int x) //转化为十进制
{
    int temp,sum;
    temp = 1;
    sum=0;
    while(x!=0)
    {
        sum+=(temp*(x%16));
        x/=16;
        temp*=16;
    }
    return sum;
}

int main()
{
    int a,b;
    int m,n;
    int i = 0;
    while(cin >> hex >> a >>b)
    {
        m = htod(a);
        n = htod(b);
        cout << m+n<<endl;
        i++;
    }
    return 0;
}
bubuko.com,布布扣

16进制A+B

原文:http://www.cnblogs.com/gaosshun/p/3541485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!