Problem H-8 Martian Addition

来源:互联网 发布:驱动精灵知乎 编辑:程序博客网 时间:2024/06/03 06:26

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:
1234567890abcdefghij99999jjjjj9999900001

Sample Output:
bdfi02467jiiiij00000
题目介绍求100位以内的两个二十进制数字的和解题思路用字符串来储存数字,两两相加,满20进一位源代码#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[210]={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;                }                if(num[i]==0)                        i-=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;        }}
根据扎实的编程基本思路,按照普通的算数运算就能达到解题的效果。
0 0
原创粉丝点击