我需要使用向量来添加2个巨大的数字,如下所示:
(示例:
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>
?
答案 0 :(得分:0)
#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;
}
答案 1 :(得分:0)
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;
}