I have to writea piece of code to calculate the factorial of somewhat large numbers so the long long type won't suffize, so using vector, I stumbled upon some issues during run time:
'#include
#include

using namespace std;

vector factorial(short n)
{
vector v, z;
do
{
short m = n%10;
v.push_back(m);
}while(n/=10);

short temp = 0;
int x;
//short m = 0;

while(n-1)
{
for(auto &w : v)
{
x = w*(n-1) + temp;
temp = x/10;
z.push_back(x%10);
}
}
return z;
}

int main()
{
short T;
cin >> T;

vector v;
short temp;
while(T--)
{
cin >> temp;
v.push_back(temp);
}

vector tmp;
for(auto &w : v)
{
tmp = factorial(w);
for(auto i=tmp.end(); i!=tmp.begin(); i--)
cout << *i;
cout << endl;
}
return 0;
}
'

Respuesta :

tonb
Your second comment is a solution that is actually almost right!!!
Here's what you should fix:

- The array v[200] should be initialized to all zeros, otherwise it may contain junk. You can simply do this by typing short v[200] = {}; right where you declare it.

- Your while loop should loop from all values of n all the way down to 1. So you need while(n>1) in stead of while(n--<=2)

- At the very end of the while loop, before its closing brace, add an n--; to decrement its value. Alternatively you could have created a do {} while construct.

That's it! After that it correctly calculates all 157 digits of 100!

100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
ACCESS MORE