大数相加

57 篇文章 3 订阅
12 篇文章 1 订阅

以前在网上看大数相加的代码,基本上都是用字符数组写的。今天突然想试试用C++中的string能不能写,写了之后感觉比用字符串写略微简单,先上代码吧

大数相加的函数

void add(string &a, string &b, string &c)
{
    int lena = a.length(), lenb = b.length();
    int i, j;
    c = "";
    string t("");
    char ch;
    int up;
    for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
    {
        ch = a[i] + b[j] - 2*'0' + up;
        up = ch / 10;
        ch %= 10;
        ch += '0';
        t += ch;
    }
    if (i < 0)
    {
        for(; j >= 0; j--)
        {
            ch = b[j] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    else if ( j < 0)
    {
        for(; i >= 0; i--)
        {
            ch = a[i] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    if (up)
    {
        ch = up + '0';
        t += ch;
    }
    for (i=t.length() - 1; i >= 0 ; i--)
    {
        c += t[i];
    }
}

string比较方便的地方是,string中有很多成员函数,而且对各种运算符都进行了重载,用起来比较方便。
比如说,复制字符串直接使用=就行了,而不用像C语言那样使用strcpy函数了。对string的操作符合我们的平时对普通数据的操作,用起来很方便,不用考虑数组溢出问题

string详解在这里http://blog.csdn.net/qq_21120027/article/details/47816273

用上面的函数直接就A了几道题


POJ 1503 Integer Inquiry http://poj.org/problem?id=1503
代码

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>   
using namespace std;
string a, b, c;

void add(string &a, string &b, string &c)
{
    int lena = a.length(), lenb = b.length();
    int i, j;
    c = "";
    string t("");
    char ch;
    int up;
    for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
    {
        ch = a[i] + b[j] - 2*'0' + up;
        up = ch / 10;
        ch %= 10;
        ch += '0';
        t += ch;
    }
    if (i < 0)
    {
        for(; j >= 0; j--)
        {
            ch = b[j] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    else if ( j < 0)
    {
        for(; i >= 0; i--)
        {
            ch = a[i] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    if (up)
    {
        ch = up + '0';
        t += ch;
    }
    for (i=t.length() - 1; i >= 0 ; i--)
    {
        c += t[i];
    }
}

int main()
{

    {
        cin >> a;
        if (a == "0")
        {
            cout << "0\n";
            return 0;
        }
        while(1)
        {
            cin >> b;
            if (b == "0")
            {
                break;
            }
            add(a, b, c);
            a = c;
        }
        cout << a << endl;
    }

    return 0;
} 

HDU 1002 A + B Problem II http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>    
using namespace std;
string a, b, c;

void add(string &a, string &b, string &c)
{
    int lena = a.length(), lenb = b.length();
    int i, j;
    c = "";
    string t("");
    char ch;
    int up;
    for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
    {
        ch = a[i] + b[j] - 2*'0' + up;
        up = ch / 10;
        ch %= 10;
        ch += '0';
        t += ch;
    }
    if (i < 0)
    {
        for(; j >= 0; j--)
        {
            ch = b[j] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    else if ( j < 0)
    {
        for(; i >= 0; i--)
        {
            ch = a[i] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    if (up)
    {
        ch = up + '0';
        t += ch;
    }
    for (i=t.length() - 1; i >= 0 ; i--)
    {
        c += t[i];
    }
}

int main()
{
    int t, i;
    cin >> t;
    for(i = 1; i <= t; i++)
    {
        a = b = c = "";
        cin >> a >> b;
        add(a, b, c);
        printf("Case %d:\n", i);
        cout << a << " + " << b << " = " << c << endl;
        if (i != t)
        {
            cout << endl;
        }
    }

    return 0;
} 

HDU-1047-Integer Inquiry http://acm.hdu.edu.cn/showproblem.php?pid=1047

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
void add(string &a, string &b, string &c)
{
    int lena = a.length(), lenb = b.length();
    int i, j;
    c = "";
    string t("");
    char ch;
    int up;
    for (up = 0, i = lena - 1, j = lenb - 1; i >= 0 && j >= 0 ; i--, j--)
    {
        ch = a[i] + b[j] - 2*'0' + up;
        up = ch / 10;
        ch %= 10;
        ch += '0';
        t += ch;
    }
    if (i < 0)
    {
        for(; j >= 0; j--)
        {
            ch = b[j] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    else if ( j < 0)
    {
        for(; i >= 0; i--)
        {
            ch = a[i] - '0' + up;
             up = ch / 10;
             ch %= 10;
             ch += '0';
             t += ch;
        }
    }
    if (up)
    {
        ch = up + '0';
        t += ch;
    }
    for (i=t.length() - 1; i >= 0 ; i--)
    {
        c += t[i];
    }
}

int main()
{
    int n, T;
    cin >> T;
    string a, b, c, t;
    while(T--)
    {
        a = "0";
        while(cin >> b)
        {
            if (b == "0")   break;
            add(a, b, c);
            a = c;
        }
        cout << a << endl;
        if(T > 0)   cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值