如何将100位整数放在循环中,并在每个周期上添加 1

How to put 100-digit integer in a loop and add +1 on each cycle?

本文关键字:周期 添加 100位 整数 循环      更新时间:2023-10-16

你好女士&绅士!我有这个数字: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;
}