《ACM程序设计》书中题目H(其实火星人有20个指头QAQ)

本文介绍了一个关于火星人使用20进制计数系统的趣味编程挑战,任务是实现两个100位20进制数的加法运算,并提供了一种使用字符数组存储数据并逐位相加的解决方案。

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

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


  这道题就是说有一群火星人举办ACM比赛,设了一道加法题,但火星人有20根手指,所以他们都用20进制;

  题目要求很简单,就是20进制的加法,输入两个数,得到一个和;

 
  大体思路是这样的:

1.因为最大可以达到100位数,所以利用20进制转化为10进制运算后再转回来的方法肯定行不通的,必须利用字符数组存储数据的思想来考虑;

2.利用两个字符数组a和b来存储输入的数,从个位开始相加,按照加法满20(j)进一的原则进行运算;

3.利用整形数组来存每一位数(比如a为10,b为11,这样做是为了方便进一的运算),然后再转化回字符型进行输出。

程序如下

#include<bits/stdc++.h>
using namespace std;
int main()
{
        char a[105],b[105],c;
        int i,j,k;
        while(cin>>a>>b)
        {
                int num[205]={0};
                j=strlen(a)-1;
                k=strlen(b)-1;    //读取两个字符串的长度,便于后面从个位开始运算
                for(i=0;j>=0||k>=0;i++)    //当两个数都累加完成时,结束循环
                {
                        if(j>=0)    //将字符串每一位的值累加到整形数组中,若累加完成,便结束
                        {
                                if(a[j]>='0'&&a[j]<='9')
                                        num[i]+=(a[j]-'0');
                                else
                                        num[i]+=(a[j]-'a'+10);
                                j--;
                        }
                        if(k>=0)    //同上
                        {
                                if(b[k]>='0'&&b[k]<='9')
                                        num[i]+=(b[k]-'0');
                                else
                                        num[i]+=(b[k]-'a'+10);
                                k--;
                        }
                        num[i+1]+=num[i]/20;
                        num[i]=num[i]%20;    //满20进一,这就是为什么要使用整形数组来存储结果的原因
                }
                if(num[i]==0)
                        i-=1;    //这是一个易错点,第一次失败的原因也在此,以下有对这里的说明(注1)
                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;
        }
}

注1:

按照以上程序,我的输出是从i到0倒序输出(因为要从最高位进行输出),但是这样有一个问题,就是如果最高位没有进一,输出时最高为就会多一个0(比如1+a=b,输出时会变成“0b”),为了防止这种情况的出现,当最高位为零时省略输出。


这道题不算难,但是有一些小问题还是蛮棘手,很难去发现,不细心可能就很难AC的哟@V@~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值