练习2014082701

/********************************************************************
* @file     Main_practise.cpp
* @date     2014-8-27
* @author   Tiger
* @brief    练习
* @details  大数加法
********************************************************************/
#include <iostream>
#include <cstring>

const int SIZE = 100;

int Add(const char* a, const char* b, char* sum);

int main(int argc, const char* argv[])
{
    char* a = "123";
    char* b = "89";

    char sum[SIZE]= {0};
    int nPos = Add(a, b, sum);

    for (int i=nPos; i<SIZE; ++i)
    {
        std::cout << sum[i];
    }
    std::cout << std::endl;

    system("pause");
    return 0;
}

int Add(const char* a, const char* b, char* sum)
{
    int i = 0, j = 0, k = 0;
    for (i=strlen(a)-1, j=SIZE-1; i>=0; --i, --j)
    {
        sum[j] = a[i] - ‘0‘;
    }
    for (i=strlen(b)-1, k=SIZE-1; i>=0; --i, --k)
    {
        sum[k] += b[i] - ‘0‘;
    }

    int nMin = (j < k ? j : k);
    for (int i=SIZE-1; i>nMin; --i)
    {
        sum[i-1] += sum[i]/10;
        sum[i] = sum[i]%10 + ‘0‘;
    }

    if (sum[nMin-1] != 0 && sum[nMin-1] != ‘0‘)
    {
        return nMin;
    }
    else
    {
        return nMin+1;
    }
}
时间: 2024-12-12 04:36:37

练习2014082701的相关文章

2014082701黑魔法师之门(C++)

#include<iostream>#define p 1000000009using namespace std;int read() { int f=0; char x;x=getchar(); while(x<'0'||x>'9') x=getchar(); while(x>='0'&&x<='9') { f=f*10+x-'0'; x=getchar(); } return f;}int fa[200001];int find(int x){re