SDNU 1280.就问你慌不慌(高精度)

Description

求N进制的高精度加法

Input

第一行输入N(2≤N≤10)

第二行两个数X Y(长度均≤100)

Output

输出N进制下X和Y的和

Sample Input

5
2 4

Sample Output

11
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define ll long long
#define eps 1e-9

const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

char ads[10000005];
char s1[10005], s2[10005];
int n;

void additive(char* a, char* b)
{
    memset(ads, 0, sizeof(ads));
    int len1, len2;
    len1 = strlen(a);
    len2 = strlen(b);
    if(len1 < len2)///保持len1是最长的
    {
        for(int i = 0; i<len2; i++)
            swap(a[i], b[i]);
        swap(len1, len2);
    }
    reverse(a, a + len1);
    reverse(b, b + len2);
    int t = 0, i;
    for(i = 0; i < len2; i++)///一遍求和一边求进制
    {
        ads[i] = ((a[i] - '0') + (b[i] - '0') + t) % n + '0';
        t = (a[i] + b[i] - '0' - '0' + t) / n;
    }
    for(int k = i; k < len1; k++)///处理较大的那个数没有加的那部分
    {
        ads[k] = (a[k] + t - '0') % n + '0';
        t = (a[k] + t - '0') / n;
    }
    if(t != 0)
        printf("%d", t);
    for(int m = len1 - 1; m >= 0; m--)
        printf("%c", ads[m]);
    printf("\n");
}

int main()
{
    scanf("%d", &n);
    cin>>s1>>s2;
    additive(s1, s2);
    return 0;
}

 

posted @ 2019-08-12 15:26  明霞  阅读(184)  评论(0编辑  收藏  举报