zoukankan      html  css  js  c++  java
  • HDU 1042 N!(高精度乘)

    Problem Description
    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
     
    Input
    One N in one line, process to the end of file.
     
    Output
    For each N, output N! in one line.
     

    题目大意:求N的阶乘。

    思路:用高精度,内存存不下这么多只能每次都重新算了……

    代码(3093MS):

      1 //模板测试
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <string>
      6 #include <algorithm>
      7 using namespace std;
      8 
      9 const int MAXN = 100010;
     10 
     11 struct bign {
     12     int len, s[MAXN];
     13 
     14     bign () {
     15         memset(s, 0, sizeof(s));
     16         len = 1;
     17     }
     18     bign (int num) { *this = num; }
     19     bign (const char *num) { *this = num; }
     20 
     21     void clear() {
     22         memset(s, 0, sizeof(s));
     23         len = 1;
     24     }
     25 
     26     bign operator = (const int num) {//数字
     27         char s[MAXN];
     28         sprintf(s, "%d", num);
     29         *this = s;
     30         return *this;
     31     }
     32     bign operator = (const char *num) {//字符串
     33         for(int i = 0; num[i] == '0'; num++) ;  //去前导0
     34         if(*num == 0) --num;
     35         len = strlen(num);
     36         for(int i = 0; i < len; ++i) s[i] = num[len-i-1] - '0';
     37         return *this;
     38     }
     39 
     40     bign operator + (const bign &b) const {
     41         bign c;
     42         c.len = 0;
     43         for(int i = 0, g = 0; g || i < max(len, b.len); ++i) {
     44             int x = g;
     45             if(i < len) x += s[i];
     46             if(i < b.len) x += b.s[i];
     47             c.s[c.len++] = x % 10;
     48             g = x / 10;
     49         }
     50         return c;
     51     }
     52 
     53     bign operator += (const bign &b) {
     54         *this = *this + b;
     55         return *this;
     56     }
     57 
     58     void clean() {
     59         while(len > 1 && !s[len-1]) len--;
     60     }
     61 
     62     bign operator * (const bign &b) {
     63         bign c;
     64         c.len = len + b.len;
     65         for(int i = 0; i < len; ++i) {
     66             for(int j = 0; j < b.len; ++j) {
     67                 c.s[i+j] += s[i] * b.s[j];
     68             }
     69         }
     70         for(int i = 0; i < c.len; ++i) {
     71             c.s[i+1] += c.s[i]/10;
     72             c.s[i] %= 10;
    
     73         }
     74         c.clean();
     75         return c;
     76     }
     77     bign operator *= (const bign &b) {
     78         *this = *this * b;
     79         return *this;
     80     }
     81 
     82     bign operator *= (const int &b) {//使用前要保证>len的位置都是空的
     83         for(int i = 0; i < len; ++i) s[i] *= b;
     84         for(int i = 0; i < len; ++i) {
     85             s[i + 1] += s[i] / 10;
     86             s[i] %= 10;
     87         }
     88         while(s[len]) {
     89             s[len + 1] += s[len] / 10;
     90             s[len] %= 10;
     91             ++len;
     92         }
     93         return *this;
     94     }
     95 
     96     bign operator - (const bign &b) {
     97         bign c;
     98         c.len = 0;
     99         for(int i = 0, g = 0; i < len; ++i) {
    100             int x = s[i] - g;
    101             if(i < b.len) x -= b.s[i];
    102             if(x >= 0) g = 0;
    103             else {
    104                 g = 1;
    105                 x += 10;
    106             }
    107             c.s[c.len++] = x;
    108         }
    109         c.clean();
    110         return c;
    111     }
    112     bign operator -= (const bign &b) {
    113         *this = *this - b;
    114         return *this;
    115     }
    116 
    117     bign operator / (const bign &b) {
    118         bign c, f = 0;
    119         for(int i = len - 1; i >= 0; i--) {
    120             f *= 10;
    121             f.s[0] = s[i];
    122             while(f >= b) {
    123                 f -= b;
    124                 c.s[i]++;
    125             }
    126         }
    127         c.len = len;
    128         c.clean();
    129         return c;
    130     }
    131     bign operator /= (const bign &b) {
    132         *this  = *this / b;
    133         return *this;
    134     }
    135 
    136     bign operator % (const bign &b) {
    137         bign r = *this / b;
    138         r = *this - r*b;
    139         return r;
    140     }
    141     bign operator %= (const bign &b) {
    142         *this = *this % b;
    143         return *this;
    144     }
    145 
    146     bool operator < (const bign &b) {
    147         if(len != b.len) return len < b.len;
    148         for(int i = len-1; i >= 0; i--) {
    149             if(s[i] != b.s[i]) return s[i] < b.s[i];
    150         }
    151         return false;
    152     }
    153 
    154     bool operator > (const bign &b) {
    155         if(len != b.len) return len > b.len;
    156         for(int i = len-1; i >= 0; i--) {
    157             if(s[i] != b.s[i]) return s[i] > b.s[i];
    158         }
    159         return false;
    160     }
    161 
    162     bool operator == (const bign &b) {
    163         return !(*this > b) && !(*this < b);
    164     }
    165 
    166     bool operator != (const bign &b) {
    167         return !(*this == b);
    168     }
    169 
    170     bool operator <= (const bign &b) {
    171         return *this < b || *this == b;
    172     }
    173 
    174     bool operator >= (const bign &b) {
    175         return *this > b || *this == b;
    176     }
    177 
    178     string str() const {
    179         string res = "";
    180         for(int i = 0; i < len; ++i) res = char(s[i]+'0') + res;
    181         return res;
    182     }
    183 };
    184 
    185 istream& operator >> (istream &in, bign &x) {
    186     string s;
    187     in >> s;
    188     x = s.c_str();
    189     return in;
    190 }
    191 
    192 ostream& operator << (ostream &out, const bign &x) {
    193     out << x.str();
    194     return out;
    195 }
    196 
    197 bign ans;
    198 
    199 void solve(int n) {
    200     ans.clear();
    201     ans.len = ans.s[0] = 1;
    202     for(int i = 2; i <= n; ++i) ans *= i;
    203     cout<<ans<<endl;
    204 }
    205 
    206 int main() {
    207     int n;
    208     while(scanf("%d", &n)!=EOF) {
    209         //cout<<f[n]<<endl;
    210         solve(n);
    211     }
    212     return 0;
    213 }
    View Code
  • 相关阅读:
    [转载]kafka分布式消息机制
    mysql partition(mysql range partition,对历史数据建分区)
    【转载】MySQL Show命令总结
    【转载】hive优化之一
    【转载】SQL必知必会点
    先行发生原则Happens-before
    指令重排序
    并发编程常见面试题
    CAS无锁机制
    锁机制
  • 原文地址:https://www.cnblogs.com/oyking/p/3270244.html
Copyright © 2011-2022 走看看