INT-2147483648 ~ 2147483647
Long long:-9223372036854775808 ~ 9223372036854775807
Unsigned int 0 ~ 4294967295
Unsigned long 0 ~ 4294967295
Unsigned long: 0 ~ 1844674407370955161
And 20! (2,432,902,008,176,640,000) has exceeded the upper limit of unsigned long, And the factorial result of 10000 is 35660 bits. Therefore, regular type variables cannot save the factorial value we want to get, how can this problem be solved?
Use an array! Int result [int] is sufficient. I will not talk about anything more.Code
/* <Br/> author: m_nvalue <br/> time: 28/03/2011 <br/> Email: shiweimin1828@163.com <br/> */<br/> # include <iostream> <br/> # include <math. h> <br/> # include <fstream> <br/> # define n 10000/10000! <Br/> using namespace STD; <br/> int * multi (int * num1, Int & size1, int * num2, int size2 ); <br/> bool nextnum2 (int * num2, int size2); <br/> int main () <br/> {<br/> int num1 [5] = {, 0}; // n <br/> int num2 [5] =, 9, 9}; // The initial size of the N-1 <br/> int size1 = 5; // num1 <br/> int * bufr; <br/> bufr = multiple (num1, size1, num2, 5); <br/> for (INT I = 1; I <N-1; I ++) <br/>{< br/> If (! Nextnum2 (num2, 5) <br/> break; <br/> bufr = multi (bufr, size1, num2, 5 ); <br/>}< br/> fstream ifs ("result.txt", IOS: Out); // export the result to a text file <br/> for (INT I = 0; I <size1; I ++) <br/>{< br/> cout <bufr [I]; <br/> ifs <bufr [I]; <br/>}< br/> ifs. close (); <br/> cout <Endl <"Total:" <size1 <Endl; <br/> system ("pause "); <br/> return 0; <br/>}< br/> int * multi (int * num1, Int & size1, int * num2, int size2) <br/>{< br/> int size = size1 + size2; <br/> int * presult = new int [size]; <br/> memset (presult, 0, size * sizeof (INT); <br/> int COUNT =-1, K, I = 0, j = 0; <br/> for (I = size2-1; i> = 0; I --) <br/> {<br/> count ++; <br/> K = count; <br/> for (j = size1-1; j> = 0; j --) <br/> presult [k ++] + = num2 [I] * num1 [J]; <br/>}< br/> for (I = 0; I <size; I ++) <br/> {<br/> If (presult [I]> = 10) <br/>{< br/> presult [I + 1] + = presult [I]/10; <br/> presult [I] % = 10; <br/>}< br/> J = 0; <br/> for (I = size-1; I> = 0; I --) <br/>{< br/> If (presult [I] = 0) <br/> continue; <br/> else <br/>{< br/> size1 = I + 1; <br/> break; <br/>}< br/> int * temp = new int [size1]; <br/> for (I = size1-1; I> = 0; I --) <br/> temp [J ++] = presult [I]; <br/> Delete [] presult; <br/> return temp; <br/>}< br/> bool nextnum2 (int * num2, int size2) <br/>{< br/> int nextnum = 0; <br/> for (INT I = 0; I <size2; I ++) <br/> {<br/> nextnum + = num2 [I] * (INT) pow (10.0, (size2-i-1); <br/>}< br/> If (nextnum <= 1) <br/> return false; <br/> nextnum-= 1; <br/> for (INT I = 0; I <size2; I ++) <br/> {<br/> num2 [I] = nextnum/(INT) Pow (10.0, size2-i-1); <br/> nextnum % = (INT) Pow (10.0, size2-i-1); <br/>}< br/> return true; <br/>}