杭电OJ1002大数据相加

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杭电OJ1002大数据相加相关的知识,希望对你有一定的参考价值。

先输入n ,代表共有几组数字,再输入n组数据

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<malloc.h>
using namespace std;
char resu[10000];
void Reverse(char a[10000])
{
    int leng = strlen(a);
    char temp;
    for (int i = 0; i < leng / 2; i++)
    {
        temp = a[leng - i - 1];
        a[leng - i - 1] = a[i];
        a[i] = temp;
    }
    return ;
}
char *toAdd(char a[10000], char b[10000])
{
    Reverse(a);
    Reverse(b);
    char min[10000], max[10000];
    
    if (strlen(a) >= strlen(b))
    {
        strcpy_s(min, b);
        strcpy_s(max, a);
    }
    else
    {
        strcpy_s(min, a);
        strcpy_s(max, b);
    }
    int jinwei = 0;
    for (int i = 0; i < (int)strlen(min); i++)
    {
        if (min[i] + max[i] - ‘0‘ - ‘0‘ + jinwei>=10)
        {
            resu[i] = min[i] + max[i] - ‘0‘ + jinwei - 10;
            jinwei = 1;
        }
        else
        {
            resu[i] = min[i] + max[i] - ‘0‘ + jinwei;
            jinwei = 0;
        }
    }
    int i;
    for (i = strlen(min); i < (int)strlen(max); i++)
    {
        if (max[i]- ‘0‘ + jinwei>=10)
        {
            resu[i] =max[i]+ jinwei - 10;
            jinwei = 1;
        }
        else
        {
            resu[i] =max[i]+ jinwei;
            jinwei = 0;
        }
    }
    if (jinwei)
    {
        resu[i] = jinwei + ‘0‘;
        resu[++i] = ‘\0‘;
    }
    else
    resu[i] = ‘\0‘;
    Reverse(resu);
    return resu;
}
int  main( )
{
    int n; char a[20][10000], b[20][10000];
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i] >> b[i];    
    }
    for (int i = 0; i < n; i++)
    {
        cout << "Case " << i+1 << ":" << endl;
        cout << a[i] << " + " << b[i] << " = ";
        toAdd(a[i], b[i]);
        int k = 0;
        while (resu[k] == ‘0‘)
            k++;
        if (k == strlen(resu))
        {
            cout << "0";
            cout << endl;
        }
        else
        {
            for (int o = k; o < strlen(resu);o++)
            cout << resu[o];
            cout << endl;
        }
        if (i <( n - 1))
            cout << endl;
    }
    return 0;
}

以上是关于杭电OJ1002大数据相加的主要内容,如果未能解决你的问题,请参考以下文章

杭电OJ 1002

杭电OJ 1002

杭电oj1002,,做完留一下记录(c)

杭电oj训练赛第四场

SOJ 1002/1003/1004 大整数相加/相乘/相除

如何有效使用 杭电 hdu oj