gpt4 book ai didi

c++ - vector 和巨大的数字

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:03 26 4
gpt4 key购买 nike

我需要使用 vector 来添加 2 个巨大的数字,如下所示:

(Example: 3049358031 + 1449238031)

我到处寻找,但我什么也没找到。

(我必须只使用 vector )

我有这段代码(不起作用):

#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;
}

关于c++ - vector 和巨大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com