ZOJ 1205 Martian Addition

简单的大数加法


#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

int convertCharToInt(char c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    else if (c >= 'a' && c <= 'j')
    {
        return (c - 'a') + 10;
    }
    else 
    {
        return 0;
    }
}
char convertIntToChar(int n)
{
    if (n >= 0 && n <= 9)
    {
        return '0' + n;
    }
    else if (n >= 10 && n <= 19)
    {
        return 'a' + (n - 10);
    }
    else
    {
        return '0';
    }
}

int main()
{
    string n1, n2;

    while (getline(cin, n1) && getline(cin, n2))
    {
        reverse(n1.begin(), n1.end());
        reverse(n2.begin(), n2.end());

        int l1 = n1.length();
        int l2 = n2.length();

        string result;
        int temp = 0;
        for (int i = 0; i < l1 || i < l2; i++)
        {
            int i1 = 0;
            int i2 = 0;

            if (i < l1)
            {
                i1 = convertCharToInt(n1[i]);
            }

            if (i < l2)
            {
                i2 = convertCharToInt(n2[i]);
            }

            int s = temp + i1 + i2;
            result += convertIntToChar(s % 20);
            temp = s / 20;
        }

        if (temp > 0)
        {
            result += convertIntToChar(temp);
        }

        reverse(result.begin(), result.end());

        printf("%s\n", result.c_str());
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值