Martian Addition

Martian Addition

时间: 1ms        内存:64M

描述:

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.

输入:

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.

输出:

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

示例输入:

1234567890
abcdefghij
99999jjjjj
9999900001

示例输出:

bdfi02467j
iiiij00000

提示:

参考答案(内存最优[752]):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char a[110],b[110],c[110];
    int s,m,n,l,i,j;
    while(scanf("%s%s",a,b)!=EOF)
    {
        s=0;
        m=strlen(a);
        n=strlen(b);
        l=m>n?m:n;
        for(i=0,j=m-1; i<l; i++,j--)
        {
            if(j<0)
                c[i]='0';
            else
                c[i]=a[j];
        }
        for(i=0,j=n-1; i<l; i++,j--)
        {
            if(j<0)
                a[i]='0';
            else
                a[i]=b[j];
        }
        for(i=0; i<l; i++)
        {
            if(a[i]>=97)
                a[i]-=87;
            else
                a[i]-='0';
            if(c[i]>=97)
                c[i]-=87;
            else
                c[i]-='0';
            if((c[i]+a[i]+s)%20<10)
                b[i]=(c[i]+a[i]+s)%20+'0';
            else
                b[i]=(c[i]+a[i]+s)%20-10+'a';
            s=(c[i]+a[i]+s)/20;
        }
        for(i=l-1; i>=0; i--)
            printf("%c",b[i]);
        printf("\n");
    }
    return 0;
}

参考答案(时间最优[0]):

#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>
using namespace std;
char str1[222],str2[222];
int main()
{
    while(cin>>str1>>str2)
    {
        int len1=strlen(str1);
        int len2=strlen(str2);
        int p1=len1-1;
        int p2=len2-1;
        int jw=0;
        stack<char> st;
        int t;
        while(p1>=0&&p2>=0)
        {
            t=jw+(str1[p1]>'9'?str1[p1]-'a'+10:str1[p1]-'0')+(str2[p2]>'9'?(str2[p2]-'a'+10):str2[p2]-'0');
            jw=t/20;
            t%=20;
            st.push(t>=10?'a'+t-10:t+'0');
            p1--,p2--;
        }
        while(p1>=0)
        {
            t=jw+(str1[p1]>'9'?str1[p1]-'a'+10:str1[p1]-'0');
            jw=t/20;
            t%=20;
            st.push(t>=10?'a'+t-10:t+'0');
            p1--;
        }
        while(p2>=0)
        {
            t=jw+(str2[p2]>'9'?(str2[p2]-'a'+10):str2[p2]-'0');
            jw=t/20;
            t%=20;
            st.push(t>=10?'a'+t-10:t+'0');
            p2--;
        }
        if(jw) st.push(jw>=10?'a'+jw-10:jw+'0');
        while(!st.empty())
        {
            printf("%c",st.top());
            st.pop();
        }
        printf("\n");
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注