如何将100位整数放在循环中,并在每个周期上添加 1
How to put 100-digit integer in a loop and add +1 on each cycle?
你好女士&绅士!我有这个数字:888888888888888888888888888888888888888888888888888888888888888888888888888我想连接9999999999999999999999999999999999999999999999999999999999999999999999999999999999.我的代码无法正常工作,请帮助我是完整的初学者,所以简单的答案很好,谢谢!
我在运行此代码时得到的结果是:
888888888888888888888888888888888888888888888888888888888888889
8888888888888888888888888888888888888888888888888888888888888801
8888888888888888888888888888888888888888888888888888888888888811
8888888888888888888888888888888888888888888888888888888888888821
而不是:
888888888888888888888888888888888888888888888888888888888888889
888888888888888888888888888888888888888888888888888888888888890
888888888888888888888888888888888888888888888888888888888888891
888888888888888888888888888888888888888888888888888888888888892
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string findSum(string str1, string str2)
{
string str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((str1[i] - '0') + (str2[i + diff] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((str2[i] - '0') + carry);
str.push_back(sum % 10 + '0');
carry = sum / 10;
}
if (carry)
str.push_back(carry + '0');
return str;
}
int main()
{
string str1 = "100000000000000000000000000000000000000000000000000000000000000";
string str2 = "888888888888888888888888888888888888888888888888888888888888888";
string strXP = "0";
for (int x = 0; x != 10; x++) {
cout << findSum(str1, str2) << endl;
str2 = findSum(str1, str2);
reverse(str2.begin(), str2.end());
}
return 0;
}
我不知道如何使用str1,我猜该错误在某个地方... :(
您需要一个BigInteger
表示类来表示大数字并在其上执行数学操作。.GNU Multiple Precision Arithmetic Library
可以做您比我们可以给您任何答案更好的您所要求的。.
但是,对于一个天真的解决方案,您可以使用以下内容,我只是为了娱乐而写的。普通添加将起作用。
注意:这不是实现BigInteger
算术的最有效方法,但是答案应该很好。如果需要效率,请查看:https://gmplib.org/
//
// main.cpp
// BigSimpleInteger
//
// Created by Brandon Anthony on 2017-09-11.
//
#include <algorithm>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <cstring>
#include <thread>
class BigInteger
{
private:
char sign;
std::string digits;
const std::size_t base = 10;
short toDigit(std::size_t index) const {return index < digits.size() ? digits[index] - '0' : 0;}
void Normalise();
inline bool isPositive() const {return sign == '+';}
inline bool isNeutral() const {return sign == '~';}
public:
BigInteger();
BigInteger(int value);
BigInteger(int64_t value);
BigInteger(const std::string &value);
BigInteger(const BigInteger &other);
bool operator == (const BigInteger &other) const;
bool operator != (const BigInteger &other) const;
BigInteger& operator = (const BigInteger &other);
BigInteger& operator += (const BigInteger &other);
friend std::ostream& operator << (std::ostream& os, const BigInteger& other);
};
BigInteger::BigInteger() : sign('~'), digits(1, '0') {}
BigInteger::BigInteger(int value) : BigInteger(static_cast<int64_t>(value)) {}
BigInteger::BigInteger(int64_t value) : sign(value == 0 ? '~' : value > 0 ? '+' : '-'), digits(std::to_string(value))
{
std::reverse(digits.begin(), digits.end());
}
BigInteger::BigInteger(const std::string &value) : sign('~'), digits(value)
{
sign = digits.empty() ? '~' : digits[0] == '-' ? '-' : '+';
if (digits[0] == '+' || digits[0] == '-') digits.erase(0, 1);
std::reverse(digits.begin(), digits.end());
Normalise();
for (std::size_t I = 0; I < digits.size(); ++I)
{
if (!isdigit(digits[I]))
{
sign = '~';
digits = "0";
break;
}
}
}
BigInteger::BigInteger(const BigInteger &other) : sign(other.sign), digits(other.digits) {}
void BigInteger::Normalise()
{
for (int I = static_cast<int>(digits.size()) - 1; I >= 0; --I)
{
if (digits[I] != '0') break;
digits.erase(I, 1);
}
if (digits.empty())
{
digits = "0";
sign = '~';
}
}
bool BigInteger::operator == (const BigInteger &other) const
{
if (sign != other.sign || digits.size() != other.digits.size())
return false;
for (int I = static_cast<int>(digits.size()) - 1; I >= 0; --I)
{
if (toDigit(I) != other.toDigit(I))
return false;
}
return true;
}
bool BigInteger::operator != (const BigInteger &other) const
{
return !(*this == other);
}
BigInteger& BigInteger::operator = (const BigInteger &other)
{
sign = other.sign;
digits = other.digits;
return *this;
}
BigInteger& BigInteger::operator += (const BigInteger &other)
{
if (other.isNeutral())
{
return *this;
}
if (sign != other.sign)
{
//return *this -= (other * -1);
throw std::runtime_error("Subtraction (Additon of a negative) not supported");
}
int carry = 0, total = 0;
std::size_t length = std::max(digits.size(), other.digits.size());
for (std::size_t I = 0; I < length; ++I)
{
total = toDigit(I) + other.toDigit(I) + carry;
carry = total / base;
total %= base;
if (I >= digits.size())
{
digits.resize(digits.size() + 1);
}
digits[I] = total + '0';
}
if (carry)
{
digits.resize(digits.size() + 1);
digits[digits.size() - 1] = carry + '0';
}
return *this;
}
std::ostream& operator << (std::ostream& os, const BigInteger& other)
{
if (other.sign == '-') os << '-';
std::string temp = other.digits;
std::reverse(temp.begin(), temp.end());
return os << temp;
}
int main(int argc, const char * argv[]) {
BigInteger bi{"888888888888888888888888888888888888888888888888888888888888888"};
BigInteger ti{"999999999999999999999999999999999999999999999999999999999999999"};
std::cout<<bi<<"n";
while (bi != ti)
{
bi += 1;
std::cout<<bi<<"n";
}
return 0;
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 将图像添加到资源文件夹UWP C++
- 如何将100位整数放在循环中,并在每个周期上添加 1