【问题标题】:Why vector in C++ doesn't resize automatically为什么 C++ 中的向量不会自动调整大小
【发布时间】:2018-02-15 07:20:40
【问题描述】:

我有一个很长的阶乘程序,需要找到高达 100 的阶乘。它可以很好地找到 33 阶乘,但不能从 34 开始。有人可以帮助确定问题。

#include <iostream>
#include <vector>
#include <utility>


using namespace std;

void bigFactorials(int n)
{
    vector<int> v;//If I specify the size as v(1000) it works fine but I don't 

    //want to specify the size beforehand.
    v.push_back(1);

    // Complete this function
    for(int i=2;i<=n;i++) {
        int carry = 0, mul=0;
        for(auto j=v.rbegin();j!=v.rend();j++) {
            mul=i**j + carry;
            carry=mul/10;
            *j=mul%10;
        }

        if(carry)
            v.insert(v.begin(),carry);    
    }

    for(int i:v)
        cout<<i;
}

int main()
{
    int n;

    cin >> n;
    if( n>0 && n<101 )
        bigFactorials(n);

    return 0;
}

【问题讨论】:

  • @StoryTeller:我不认为他想存储 34!在int。看起来他正在尝试使用向量将其存储在基本的 BigInt 实现中。
  • mul = i**j + carry; 应该做什么?
  • @Bob__ 取消引用迭代器 j 并乘以 i?
  • #include&lt;bits/stdc++.h&gt; 不要这样做,这是一个内部头文件。它的唯一目的是包含using namespace std;
  • unsigned long 用于unsigned long carry = 0, mul=0; 你会得到i**j + carry 的溢出。如果你想达到 100,你需要 tong。

标签: c++ c++11 vector factorial


【解决方案1】:

问题是当进位> 10,然后你插入一个整数值而不将它拆分成字符,它应该如下实现

if(carry)
{
    if (carry >= 10)
    {
        while (carry > 0)
        {
            v.insert(v.begin(),carry % 10); // put each char from carry into v
            carry = carry / 10;
        }
    }
    else
        v.insert (v.begin(),carry);
}

有了这个,您甚至可以将v 保留为vector&lt;char&gt; 和 50!我们有 30414093201713378043612608166064768844377641568960512000000000000。

【讨论】:

  • 已验证多达 99 个!
  • 感谢@rafix07 的回答。
【解决方案2】:

当我打印出添加到向量中的carry 值时,您肯定会溢出:

1
5
4
3
3
3
4
6
8
13
20
35
64
121
243
510
1124
2585
6204
15511
40329
108888
304888
884176
2652528
8222838
26313083
86833176
-134263930
-134263930-639604140847618609643520000000

我切换到long long 用于向量和carrymul 并且能够得到正确答案 34!,但 99!仍然溢出。似乎您需要进一步减少要添加到向量中的数字。

【讨论】:

    【解决方案3】:

    您需要将carry 拆分为十进制数字并插入到您的 BigInt 前面。

    while (carry)
    {
        v.insert(v.begin(), carry % 10);
        carry /= 10;
    }
    

    这里是整个函数:

    void bigFactorials(int n) {
        vector<int> v;//If I specify the size as v(1000) it works fine but I don't 
        //want to specify the size beforehand.
        v.push_back(1);
    
        // Complete this function
        for (int i = 2; i <= n; i++)
        {
            int carry = 0, mul = 0;
            for (auto j = v.rbegin(); j != v.rend(); j++)
            {
                mul = i * *j + carry;
                carry = mul / 10;
                *j = mul % 10;
            }
    
            while (carry)
            {
                v.insert(v.begin(), carry % 10);
                carry /= 10;
            }
        }
    
        for (int i : v)
            cout << i;
    }
    

    Live Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多