ZOJ Martian Addition

ZOJ Martian Addition

Time Limit: 2 Sec  Memory Limit: 128 MB

Submit: 30  Solved: 14

[Submit][Status][Web Board]

Description

 In the 22nd Century, scientists have discovered intelligent residents live on the Mars.

Martians are very fond of mathematics. Every year, they would hold an Arithmetic

 Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time.

This year they also invite people on Earth to join the contest.

  As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind.

 Fortunately you have taken your laptop computer with you which can help you do the job

quickly. Now the remaining problem is only to write a short program to calculate the sum

of 2 given numbers. However, before you begin to program,

you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input

You're given several pairs of Martian numbers, each number on a line.

Martian number consists of digits from 0 to 9, and lower case letters from a to j

(lower case letters starting from a to present 10, 11, ..., 19).

The length of the given number is never greater than 100.

Output

For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input

1234567890

abcdefghij

99999jjjjj

9999900001

Sample Output

bdfi02467j

iiiij00000

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[105];
int l1,l2;
char ch1[105],ch2[105];
int work(char ch)
{
    if (ch>='0' && ch<='9') return ch-'0';
    if (ch>='a') return ch-'a'+10;
}
char solve(int k)
{
    if (k<=9) return (char)(k+48);
     else return (char)(k+97-10);
}
int main()
{
    while(~scanf("%s",&ch1))
    {
        l1=strlen(ch1);
        scanf("%s",&ch2);
        l2=strlen(ch2);
        memset(a,0,sizeof(a));
        if (l1<l2) {swap(l1,l2);swap(ch1,ch2);}
        int k=l1-l2;
        for(int i=l1;i>0;i--) a[i]=work(ch1[i-1]);
        for(int i=l2-1;i>=0;i--)
        {
            int y=work(ch2[i]);
            a[i+k+1]+=y;
        }

        for(int i=l1;i>0;i--)
        {
            a[i-1]+=a[i]/20;
            a[i]=a[i]%20;
        }
       // for(int i=0;i<=l1;i++) printf("%d ",a[i]);
        if (a[0]>0) printf("%c",solve(a[0]));
        for(int i=1;i<=l1;i++) printf("%c",solve(a[i]) );
       // for(int i=1;i<=l1;i++) printf("%d",a[i]);
          printf("\n");
    }
    return 0;
}

 

posted on 2016-07-18 21:10  Yxter  阅读(200)  评论(0编辑  收藏  举报

导航