16进制A+B

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

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

#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;
}

 

posted on 2014-02-09 15:37  acm_shun  阅读(374)  评论(0编辑  收藏  举报

导航