HDU1720 A+B Coming

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

Author
威士忌

Source
HZIEE 2007 Programming Contest

康复训练第二题。
题意:计算两个十六进制数相加。hexadecimal是十六进制。
思路:很尴尬的一直在wa,后来发现,原来16进值是可以多位数的。就相当于十进制数可以输入111和222相加。所以代码就不能用char来接收a和b两个输入,因为不够啊。所以要用string。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int zhengling(char a)
{
    if(a-'0'<10) return a = a-'0';
    else if (a<'a') return a = a-'A'+10;
    else return a = a-'a'+10;
}
int main()
{
    string a,b;
    while(cin >> a >> b)
    {
        int numa = 0,numb = 0;
        for(int i = 0;i<a.length();i++)
        numa=numa*16+zhengling(a[i]);
        for(int i = 0;i<b.length();i++)
        numb=numb*16+zhengling(b[i]);
        cout<<numa+numb<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值