ZOJ 1205 Martian Addition

来源:互联网 发布:淘宝截止2016 编辑:程序博客网 时间:2024/09/21 09:18

简单的大数加法


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



原创粉丝点击