《ACM程序设计》书中题目H

本文介绍了一个有趣的编程挑战——为火星算术竞赛编写一个程序来计算20进制下两个大数的和。参赛者需要处理特殊的20进制数字表示,并实现有效的加法运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

求20进制两个数的和(又没用stl。。。。。)

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char c[100],d[100],g[100],f[100];
    int a,b,t,e;
    while(gets(c) && gets(d) && c[0]!=0 && d[0]!=0)
    {
        int i,k=0;
        char *p;
        a=strlen(c);
        b=strlen(d);
        if(a>b)
        {
            t=a;
            e=b;
            p=d;
        }
        else
        {
            t=b;
            e=a;
            p=c;
        }
        strcpy(g,p);
        for(i=0;i<t-e;i++)
            *p++=48;
        *p=0;
        strcat(p,g);
        for(i=t-1;i>=0;i--)
        {
            if(c[i]>=97)
                c[i]-=87;
            else
                c[i]-=48;
            if(d[i]>=97)
                d[i]-=87;
            else
                d[i]-=48;
            f[i]=c[i]+d[i]+k;
            if(f[i]>29)
            {
                f[i]+=67;
                k=1;
            }
            else
                if(f[i]>19)
                {
                    f[i]+=28;
                    k=1;
                }
                else
                    if(f[i]>=10)
                    {
                        f[i]+=87;
                        k=0;
                    }
                    else
                    {
                        f[i]+=48;
                        k=0;
                    }
        }
        f[t]=0;
        if(k==1)
            printf("1");
        printf("%s\n",f);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值