Why vector in C++ doesn't resize automatically

后端 未结 3 575
眼角桃花
眼角桃花 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 00:59

    You are definitely overflowing, when I print out the carry values that are added to the vector:

    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
    

    I switched to long long for the vector and for carry and mul and was able to get the correct answer for 34!, but 99! still overflows. Seems you need to further reduce the numbers you are adding to the vector.

提交回复
热议问题