HDU1042 N!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

  
  
  
  
  1. #include<iostream> 
  2. #include<stdio.h> 
  3. #include<string> 
  4. #include<iomanip> 
  5. #include<algorithm> 
  6. using namespace std;  
  7.  
  8. const int MAX_GROUPS = 10000;//最多万组,每组最多位整数,即最多可容纳万位整数  
  9. const int MAXN = 9999;//每组的上限值  
  10. const int GROUP_LEN = 4;//每组的最大长度  
  11.  
  12. class BigInteger  
  13. {  
  14. private:  
  15.     int data[MAX_GROUPS];  
  16.     int len;  
  17.     void init()  
  18.     {  
  19.         memset(data,0,sizeof(data));  
  20.     }  
  21. public:  
  22.     BigInteger()  
  23.     {  
  24.         init();  
  25.         len = 0;  
  26.     }  
  27.     BigInteger(const int b);  
  28.     BigInteger(const BigInteger &);  
  29.  
  30.     bool operator > (const BigInteger&)const;  
  31.     BigInteger & operator=(const BigInteger &);  
  32.     BigInteger & add(const BigInteger &);  
  33.     BigInteger & sub(const BigInteger &);  
  34.     BigInteger operator+(const BigInteger &) const;  
  35.     BigInteger operator-(const BigInteger &) const;  
  36.     BigInteger operator*(const BigInteger &) const;  
  37.     BigInteger operator/(const int &) const;  
  38.     void print();  
  39. };  
  40. BigInteger::BigInteger(const int num)  
  41. {  
  42.     int res,tmp = num;  
  43.     len = 0;  
  44.     init();  
  45.     while(tmp > MAXN)  
  46.     {  
  47.         res = tmp - tmp / (MAXN + 1) * (MAXN + 1);  
  48.         tmptmp = tmp / (MAXN + 1);  
  49.         data[len++] = res;  
  50.     }  
  51.     data[len++] = tmp;  
  52. }  
  53. BigInteger::BigInteger(const BigInteger & rhs) : len(rhs.len)  
  54. {  
  55.     int i;  
  56.     init();  
  57.     for(i = 0 ; i < len ; i++)  
  58.     {  
  59.         data[i] = rhs.data[i];  
  60.     }  
  61. }  
  62. bool BigInteger::operator > (const BigInteger &rhs)const  
  63. {  
  64.     int ln;  
  65.     if(len > rhs.len)  
  66.     {  
  67.         return true;  
  68.     }  
  69.     else if(len < rhs.len)  
  70.     {  
  71.         return false;  
  72.     }  
  73.     else if(len == rhs.len)  
  74.     {  
  75.         ln = len - 1;  
  76.         while(data[ln] == rhs.data[ln] && ln >= 0)   
  77.         {  
  78.             ln--;  
  79.         }  
  80.         if(ln >= 0 && data[ln] > rhs.data[ln])   
  81.         {  
  82.             return true;  
  83.         }  
  84.         else   
  85.         {  
  86.             return false;  
  87.         }  
  88.     }  
  89.  
  90. }  
  91.  
  92. BigInteger & BigInteger::operator = (const BigInteger &rhs)  
  93. {  
  94.     init();  
  95.     len = rhs.len;  
  96.     for(int i = 0 ; i < len ; i++)  
  97.     {  
  98.         data[i] = rhs.data[i];  
  99.     }  
  100.     return *this;  
  101. }  
  102. BigInteger& BigInteger::add(const BigInteger &rhs)  
  103. {  
  104.     int i,nLen;  
  105.  
  106.     nLen = rhs.len > len ? rhs.len : len;  
  107.     for(i = 0 ; i < nLen ; i++)  
  108.     {  
  109.         data[i] = data[i] + rhs.data[i];  
  110.         if(data[i] > MAXN)  
  111.         {  
  112.             data[i + 1]++;  
  113.             data[i] = data[i] - MAXN - 1;  
  114.         }  
  115.     }  
  116.     if(data[nLen] != 0)   
  117.     {  
  118.         len = nLen + 1;  
  119.     }  
  120.     else   
  121.     {  
  122.         len = nLen;  
  123.     }  
  124.  
  125.     return *this;  
  126. }  
  127. BigInteger & BigInteger::sub(const BigInteger &rhs)  
  128. {  
  129.     int i,j,nLen;  
  130.     if (len > rhs.len)  
  131.     {  
  132.         for(i = 0 ; i < nLen ; i++)  
  133.         {  
  134.             if(data[i] < rhs.data[i])  
  135.             {  
  136.                 j = i + 1;  
  137.                 while(data[j] == 0) j++;  
  138.                 data[j]--;  
  139.                 --j;  
  140.                 while(j > i)  
  141.                 {  
  142.                     data[j] += MAXN;  
  143.                     --j;  
  144.                 }  
  145.                 data[i] = data[i] + MAXN + 1 - rhs.data[i];  
  146.             }  
  147.             else   
  148.             {  
  149.                 data[i] -rhs.data[i];  
  150.             }  
  151.         }  
  152.         len = nLen;  
  153.         while(data[len - 1] == 0 && len > 1)   
  154.         {  
  155.             --len;      
  156.         }  
  157.     }  
  158.     else if (len == rhs.len)  
  159.     {  
  160.         for(i = 0 ; i < len ; i++)  
  161.         {  
  162.             data[i] -rhs.data[i];  
  163.         }  
  164.         while(data[len - 1] == 0 && len > 1)   
  165.         {  
  166.             --len;      
  167.         }  
  168.     }  
  169.     return *this;  
  170. }  
  171. BigInteger BigInteger::operator+(const BigInteger & n) const   
  172. {  
  173.     BigInteger a = *this;  
  174.     a.add(n);  
  175.     return a;  
  176. }  
  177. BigInteger BigInteger::operator-(const BigInteger & T) const  
  178. {  
  179.     BigInteger b = *this;  
  180.     b.sub(T);  
  181.     return b;  
  182. }  
  183. BigInteger BigInteger::operator * (const BigInteger &rhs) const  
  184. {  
  185.     BigInteger result;  
  186.     int i,j,up;  
  187.     int temp,temp1;  
  188.  
  189.     for(i = 0; i < len; i++)  
  190.     {  
  191.         up = 0;  
  192.         for(j = 0; j < rhs.len; j++)  
  193.         {  
  194.             temp = data[i] * rhs.data[j] + result.data[i + j] + up;  
  195.             if(temp > MAXN)  
  196.             {  
  197.                 temptemp1 = temp - temp / (MAXN + 1) * (MAXN + 1);  
  198.                 up = temp / (MAXN + 1);  
  199.                 result.data[i + j] = temp1;  
  200.             }  
  201.             else   
  202.             {  
  203.                 up = 0;  
  204.                 result.data[i + j] = temp;  
  205.             }  
  206.         }  
  207.         if(up != 0)  
  208.         {  
  209.             result.data[i + j] = up;  
  210.         }  
  211.     }  
  212.     result.len = i + j;  
  213.     while(result.data[result.len - 1] == 0 && result.len > 1) result.len--;  
  214.     return result;  
  215. }  
  216. BigInteger BigInteger::operator/(const int & b) const  
  217. {  
  218.     BigInteger ret;  
  219.     int i,down = 0;  
  220.  
  221.     for(i = len - 1 ; i >= 0 ; i--)  
  222.     {  
  223.         ret.data[i] = (data[i] + down * (MAXN + 1)) / b;  
  224.         down = data[i] + down * (MAXN + 1) - ret.data[i] * b;  
  225.     }  
  226.     ret.len = len;  
  227.     while(ret.data[ret.len - 1] == 0) ret.len--;  
  228.     return ret;  
  229. }  
  230. void BigInteger::print()  
  231. {  
  232.     int i;  
  233.  
  234.     cout << data[len - 1];  
  235.     for(i = len - 2 ; i >= 0 ; i--)  
  236.     {  
  237.         cout.width(GROUP_LEN);  
  238.         cout.fill('0');  
  239.         cout << data[i];  
  240.     }  
  241.     cout << endl;  
  242. }  
  243. int main()  
  244. {  
  245.     int i,n;  
  246.     BigInteger result,num;  
  247.  
  248.     while(scanf("%d",&n)!=EOF)  
  249.     {  
  250.         result = BigInteger(1);  
  251.         for(i = 2;i <= n; ++i)  
  252.         {  
  253.             num = BigInteger(i);  
  254.             resultresult = result * num;  
  255.         }  
  256.         result.print();  
  257.     }  
  258.     return 0;  

 

你可能感兴趣的:(职场,ACM,休闲)