hdu 1720

来源:互联网 发布:linux系统启动自检 编辑:程序博客网 时间:2024/03/29 18:14

把十六进制转化为十进制,并进行加法运算

#include<iostream>
#include<string>
using namespace std;
int fun(char ch)
{
    if(ch>='0'&&ch<='9')return ch-'0';
    else
    {
        ch=toupper(ch);
        return ch-'A'+10;
    }
}
int fun2(int k)
{
    int i,ans=1;
    for(i=1;i<=k;i++)
    {
        ans*=16;
    }
    return ans;
}
int main()
{
    int n,i,j,lenth1,lenth2,a,b;
    char ch1[1000],ch2[1000];
    while(cin>>ch1>>ch2)
    {
        lenth1=strlen(ch1);
        lenth2=strlen(ch2);
        a=b=0;
        for(i=0,j=lenth1-1;i<lenth1;i++,j--)
        {
            a+=fun(ch1[i])*fun2(j);
        }
        for(i=0,j=lenth2-1;i<lenth2;i++,j--)
        {
            b+=fun(ch2[i])*fun2(j);
        }
        cout<<a+b<<endl;
    }
    system("pause");
    return 0;
}

原创粉丝点击