矢量和巨大的数字
Vectors and huge numbers
我需要使用向量来添加两个巨大的数字,像这样:
(例如:
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;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 在linux上调试巨大的C++项目
- 将数字转换为字母(例如:123 转换为一二三)
- 将数字提高到一个巨大的指数
- 未来值公式显示疯狂巨大数字的输出
- 我的函数总是返回一个巨大的数字
- std::queue::size() 可以在 size() == 0 的 pop() 之后返回一个巨大的数字
- 求和巨大的数字
- 矢量和巨大的数字
- FreeType2 ft_outline_decomposition返回巨大的数字
- 计算pow(double,2)得到一个巨大的数字(C)