ZOJ Martian Addition (20進位制的兩個大數相加)

ZMST發表於2018-10-23

題目連結:

#include <cstdio>
#include <cmath>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<string>
#include<iostream>
#include<set>
#include<vector>
using namespace std;
int main()
{
    char s1[110],s2[110];
    while(cin>>s1>>s2)
    {
        int num[220];
        memset(num,0,sizeof(num));
        int len1=strlen(s1)-1;
        int len2=strlen(s2)-1;
        int i;
        for(i=0;len1>=0||len2>=0;i++)
        {
            if(len1>=0)
            {
                if(s1[len1]>='0'&&s1[len1]<='9')
                {
                    num[i]+=(s1[len1]-'0');
                }
                else
                    num[i]+=(s1[len1]-'a'+10);
                len1--;
            }
            if(len2>=0)
            {
                if(s2[len2]>='0'&&s2[len2]<='9')
                {
                    num[i]+=(s2[len2]-'0');
                }
                else
                    num[i]+=(s2[len2]-'a'+10);
                len2--;
            }
            num[i+1]+=num[i]/20;
            num[i]=num[i]%20;
        }
        if(num[i]==0)
            i--;
        char c;
        for(;i>=0;i--)
        {
            if(num[i]>=0&&num[i]<=9)
                c=num[i]+'0';
            else
                c=num[i]-10+'a';
            cout<<c;
        }
        cout<<endl;
    }
    return 0;
}

 

相關文章