ZOJ-Problem Set - 1205(大数运算,二十进制加法,水)

版权声明:本人大三在读,有错误烦请指正,共同进步- ( ゜- ゜)つロ 乾杯~点赞请按右上角,转载请标明出处: https://blog.csdn.net/hzyhfxt/article/details/83151496

Martian Addition


Time Limit: 2 Seconds      Memory Limit: 65536 KB


  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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn = 100 + 5;
using namespace std;
typedef long long ll;
char x[maxn],y[maxn];
int a[maxn],b[maxn],ans[maxn];
int ans_len = 0;
void cha_to_in(char x[],int len,int a[])
{
    int t = 0;
    og(i,len-1,0)
    {
        if(x[i] >= 'a' && x[i] <= 'z')
            a[t] = x[i] - 'a' + 10;
        else
            a[t] = x[i] - '0';
        t++;
    }
}
void cul_add(int a[],int b[],int max_len,int c[])
{
    int t = 0;
    go(i,0,max_len-1)
    {
        c[i] += a[i] + b[i];
        if(c[i] >= 20)
        {
            c[i] -= 20;
            c[i+1]++;
        }
        t++;
    }
    if(c[max_len] != 0) max_len++;
    ans_len = max_len;
}
int main()
{
   while(scanf("%s %s",x,y) != EOF)
   {
       mem(a,0);
       mem(b,0);
       mem(ans,0);
       int len1 = strlen(x), len2 = strlen(y);
       cha_to_in(x, len1,a);
       cha_to_in(y, len2,b);
       cul_add(a,b,max(len1,len2),ans);
       og(i,ans_len-1,0)
       {
           if(ans[i] >= 10)
               printf("%c",ans[i] - 10 + 'a');
           else
               printf("%c",ans[i] + '0');
       }
       cout<<endl;
   }
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/83151496