如何在循环中放入100位整数并在每个循环中添加+1?

时间:2017-09-11 13:26:06

标签: c++

Hello lady&绅士! 我有这个号码: 888888888888888888888888888888888888888888888888888888888888888 并且我希望通过在每次循环循环并且打印出所有数字时添加+1的for循环来达到9999999999999999999999999999999999999999999,然后程序应该停止。 我的代码工作不正常,请帮助我完全初学者这么简单的答案是好的,提前谢谢!

我运行此代码时获得的结果是:

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,我猜错误是在某处...... :(

1 个答案:

答案 0 :(得分:2)

你需要一个BigInteger表示类来表示大数字并对它们执行数学运算。GNU Multiple Precision Arithmetic Library能够比我们给你的任何答案做得更好。< / p>

但是,对于一个天真的解决方案,你可以使用我为了好玩而写的以下内容。它的工作原理:D我只支持添加这个答案而且它不支持添加“减号”,即减法。简单的添加将起作用。

注意:它不是实现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;
}