矢量和巨大的数字

Vectors and huge numbers

本文关键字:数字 巨大      更新时间:2023-10-16

我需要使用向量来添加两个巨大的数字,像这样:

(例如:3049358031 + 1449238031)

我到处都找了,但什么也没找到。

(我必须只用向量)

我有这个代码(这是不工作):

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector <int> int1;
    vector <int> int2;
    vector <int> final;
    int input1, input2;
    int length = 0, length1 = 0;

    cin >> input1 >> input2;
    cout << "after cin" << endl;
string strNum = to_string(input1);
length = strNum.length();
string strNum1 = to_string(input2);
length1 = strNum.length();
    if(length > length1){
        strNum = to_string(input1);
length = strNum.length();
    } else {
        strNum1 = to_string(input2);
length1 = strNum.length();
    }

    cout << length;
    string q = to_string(input2);
    for(int i = 0; i < length; i++){
        int1[i] = strNum.at(i);
        int2[i] = strNum1.at(i);
    }
    cout << "after ye" << endl;
    for(int i = 0; i < length; i++){
        cout << " " << int1[i];
    }
    return 0;
}

我需要用vector<long long>还是vector<int>

#include <vector>
#include <limits>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <stdexcept>
using namespace std;
vector<unsigned> stringToVector(string representation) {
    const unsigned DIGITS_LIMIT = numeric_limits<unsigned>::digits10;
    for (size_t i = DIGITS_LIMIT; i < representation.size(); i += DIGITS_LIMIT + 1)
        representation.insert(i, 1, ' ');
    vector<unsigned> literal;
    stringstream representationStream(representation);
    do {
        unsigned value;
        representationStream >> value;
        literal.push_back(value);
    } while (!representationStream.eof());
    return literal;
}
vector<unsigned> operator + (const vector<unsigned> & x, const vector<unsigned> & y) {
    vector<unsigned> accumulator = (x.size() > y.size())? x : y;
    const vector<unsigned> &increment = (x.size() < y.size())? x : y;
    const unsigned LIMIT = static_cast<unsigned>(pow(10, numeric_limits<unsigned>::digits10));
    const unsigned LITTLE_SIZE = min(accumulator.size(), increment.size());
    for (size_t i = 0; i < LITTLE_SIZE; ++i) {
        const unsigned UNTIL_LIMIT = LIMIT - accumulator[i];
        if (UNTIL_LIMIT > increment[i])
            accumulator[i] += increment[i]; 
        else {
            accumulator[i] = increment[i] - UNTIL_LIMIT;
            size_t j;
            for (j = i + 1; j < accumulator.size() && accumulator[j] == LIMIT; ++j)
                accumulator[j] = 0;
            if (j < accumulator.size())
                ++accumulator[j];
            else
                accumulator.push_back(1);
        }
    }
    return accumulator;
}
inline istream &operator >> (istream & in, vector<unsigned> & bigInteger) {
    string str;
    if (in >> str)
        bigInteger = stringToVector(str);
    else
        throw runtime_error("Input big integer failure"); // TODO treat this failure
    return in;
}
inline ostream &operator << (ostream & out, const vector<unsigned> & result) {
    for (vector<unsigned>::const_reverse_iterator it = result.rbegin(); it != result.rend(); ++it)
        out << *it;
    return out;
}

main() {
    vector<unsigned> x, y, result;
    cout << "x = ";
    cin >> x;
    cout << "y = ";
    cin >> y;
    result = x + y;
    cout << x << " + " << y << " = " << result << endl;
}

MPZ是你的朋友:

设置你的大数,例如

 mpz_set_str (MP_INT *integer, char *initial_value, int base)

做你的事

  std::string add_large( char const * in1, char const * in2, int const SIZE)
  {
  MP_INT integ1, integ2, result;
  mpz_set_str (&integ1, in2, 10);
  mpz_set_str (&integ2, in2, 10);
  mpz_add (&result, &integ1, &integ2);
  char output[SIZE];
  mpz_get_str (output, 10, &result);
  return output;
  }