Why vector in C++ doesn't resize automatically

后端 未结 3 620
眼角桃花
眼角桃花 2021-01-17 00:25

I\'ve a very long factorial program which needs to find factorial up to 100. It works well up to 33 factorial but not from 34. Can someone help in identifying the problem.

3条回答
  •  春和景丽
    2021-01-17 01:06

    Problem is when carry > 10, then you insert one integer value without splitting it into chars, it should be implemented as follows

    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);
    }
    

    with this you can even keep v as vector and for 50! we have 30414093201713378043612608166064768844377641568960512000000000000.

提交回复
热议问题