【t079】火星上的加法运算

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

最近欢欢看到一本有关火星的书籍,其中她被一个加法运算所困惑,由于她的运算水平有限,想向你求助,作为一名优秀的程序员
,你当然义不容辞。
【输入格式】

输入文件第一行输入一个运算的进制N(2<=n<=36),接下来的两行为需要进行运算的字符,其中每个字符串的长度不超过200
位,其为N进制的数,其中包括0-9以及a-z(代表10-35)。

【输出格式】

  输出文件内容为在N进制下这两个数的和。

Sample Input

20
1234567890
abcdefghij

Sample Output

bdfi02467j

Sample Input2

20
99999jjjjj
9999900001

Sample Output2

iiiij00000

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t079

【题解】

N维的高精度加法;
会十进制的高精度加法的话;这个也不会难;

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 300;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
string s1,s2;
int a[MAXN],b[MAXN],c[MAXN];

void change(string s1,int a[MAXN],int len1)
{
    rep1(i,1,len1)
    if (s1[i-1]>='a' && s1[i-1]<='z')
        a[i] = s1[i-1]-'a'+10;
    else
        a[i] = s1[i-1]-'0';
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    rei(n);
    cin>>s1;
    cin>>s2;
    reverse(s1.begin(),s1.end());
    reverse(s2.begin(),s2.end());
    int len1 = s1.size();
    change(s1,a,len1);
    int len2 = s2.size();
    change(s2,b,len2);
    int len = max(len1,len2);
    int x = 0;
    rep1(i,1,len)
    {
        c[i] = a[i]+b[i]+x;
        x = c[i]/n;
        c[i] = c[i] % n;
    }
    while (x>0)
    {
        len++;
        c[len] = x;
        x = c[len]/n;
        c[len] = c[len] % n;
    }
    rep2(j,len,1)
    {
        char key;
        if (c[j]>=10)
                key = c[j]-10+'a';
            else
                key = '0'+c[j];
        putchar(key);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626887.html