zoukankan      html  css  js  c++  java
  • 【codevs4165】​高精度求阶乘

    problem

    solution

    codes

    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    using namespace std;
    struct Bigint{
        vector<int>s;
        int is_minus;
        Bigint(long long num = 0, int ok = 1){ *this = num; is_minus = ok;}
        Bigint operator = (long long num){
            s.clear();
            do{
                s.push_back(num%10);
                num /= 10;
            }while(num > 0);
            return *this;
        }
        Bigint operator = (const string& str){
            s.clear();
            for(int i = str.size()-1; i >= 0; i--)
                if(str[i] == '-')is_minus = -1;
                else s.push_back(str[i]-'0');
        }
        Bigint operator + (const Bigint& b)const{
            Bigint c;  c.s.clear();
            Bigint n = *this, m = b;
            if(n.is_minus==-1 && m.is_minus==1){
                c = m-n;
            }else if(n.is_minus==1 && m.is_minus==-1){
                c = n-m;
            }else {
                if(n.is_minus==-1 && m.is_minus==-1) c.is_minus = -1;
                int x = 0, len = max(n.s.size(),m.s.size());//x是进位
                for(int i = 0; i < len; i++){
                    if(len >= n.s.size())n.s.push_back(0);//不够长的话补0
                    if(len >= m.s.size())m.s.push_back(0);
                    c.s.push_back(n.s[i]+m.s[i]+x);
                    x = c.s[i]/10;
                    c.s[i] %= 10;
                }
                if(x)c.s.push_back(x);//最高进位
                len = c.s.size()-1;
                while(c.s[len]==0 && len>0)len--;
                c.s.resize(len+1);
            }
            return c;
        }
        Bigint operator - (const Bigint& b)const{
            Bigint n = *this, m = b;
            int ok = 1; //保证被减数大于减数
            if(n.s.size()<m.s.size() || n.s.size()==m.s.size()&&n.s<m.s){
                swap(n.s, m.s);
                ok = -1;
            }
            Bigint c; c.s.clear();
            int len = max(n.s.size(), m.s.size());
            n.s.resize(len); m.s.resize(len); c.s.resize(len);//改变大小自动补0防止内存错误
            for(int i = 0; i < len; i++){
                if(n.s[i] < m.s[i]){
                    n.s[i] += 10;
                    n.s[i+1]--;
                }
                c.s[i] = n.s[i]-m.s[i];
            }
            len--;//最后一位的坐标
            while(c.s[len]==0 && len>0)len--;//删去多余的0
            c.s.resize(len+1);
            //c.s.back() *= ok;
            c.is_minus = ok;
            return c;
        }
        Bigint operator * (const Bigint& b)const{
            Bigint n = *this, m = b;
            n.s.insert(n.s.begin(),0); m.s.insert(m.s.begin(), 0);
            Bigint c; c.s.clear();
            if(n.is_minus==-1 && m.is_minus==1)c.is_minus = -1;
            if(n.is_minus==1 && m.is_minus==-1)c.is_minus = -1;
            c.s.resize(n.s.size()+m.s.size());
            for(int i = 1; i < n.s.size(); i++){
                int x = 0; //进位
                for(int j = 1; j < m.s.size(); j++){
                    c.s[i+j-1] += n.s[i]*m.s[j] + x;//原数+当前乘积+上次乘机进位
                    x = c.s[i+j-1]/10;
                    c.s[i+j-1] %= 10;
                }
                c.s[i+m.s.size()-1] = x;//进位
            }
            c.s.erase(c.s.begin());
            int len = c.s.size();
            while(c.s[len]==0 && len>0) len--; //删除多余的0
            c.s.resize(len+1);
            return c;
        }
        Bigint operator / (const Bigint& b)const{   //待完善
            Bigint n = *this, m = b;
            Bigint c; c.s.clear();
            int len = n.s.size()+m.s.size()-1;
            for(int i = len-1; i >= 0; i--){
    
            }
    
            return c;
        }
        Bigint operator % (const Bigint& b)const{   //待完善
            Bigint c; c.s.clear();
    
            return c;
        }
        Bigint operator += (const Bigint& b){
            *this = *this+b;
        }
        Bigint operator -= (const Bigint& b){
            return (*this-b);
        }
        Bigint operator *= (const Bigint& b){
            *this = *this*b;
        }
        Bigint operator /= (const Bigint& b){
            *this = *this/b;
        }
        Bigint operator %= (const Bigint& b){
            *this = *this%b;
        }
        bool operator < (const Bigint& b)const{
            if(is_minus != b.is_minus)return is_minus<b.is_minus;
            if(s.size() != b.s.size())return s.size()<b.s.size();
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] != b.s[i])return s[i]<b.s[i];
            return false;
        }
        bool operator > (const Bigint& b)const{
            if(is_minus != b.is_minus)return is_minus>b.is_minus;
            if(s.size() != b.s.size())return s.size()>b.s.size();
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] != b.s[i])return s[i]>b.s[i];
            return false;
        }
        bool operator <= (const Bigint& b)const{
            if(is_minus != b.is_minus)return is_minus<b.is_minus;
            if(s.size() != b.s.size())return s.size()<b.s.size();
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] != b.s[i])return s[i]<b.s[i];
            return true;
        }
        bool operator >= (const Bigint& b)const{
            if(is_minus != b.is_minus)return is_minus>b.is_minus;
            if(s.size() != b.s.size())return s.size()>b.s.size();
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] != b.s[i])return s[i]>b.s[i];
            return true;
        }
        bool operator == (const Bigint& b)const{
            if(is_minus != b.is_minus)return false;
            if(s.size() != b.s.size())return false;
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] != b.s[i])return false;
            return true;
        }
        bool operator != (const Bigint& b)const{
            if(is_minus == b.is_minus)return false;
            if(s.size() != b.s.size())return true;
            for(int i = s.size()-1; i >= 0; i--)
                if(s[i] == b.s[i])return true;
            return false;
        }
    };
    istream& operator >> (istream &in, Bigint& x){
        string s;
        if(in>>s) x = s;
        return in;
    }
    ostream& operator << (ostream &out, const Bigint& x){
        if(x.is_minus == -1)out<<-1*x.s[x.s.size()-1];
        else out<<x.s[x.s.size()-1];
        for(int i = x.s.size()-2; i >= 0; i--)out<<x.s[i];
        return out;
    }
    
    int main(){
        Bigint ans = 1;
        int n;
        cin>>n;
        if(n == 29){
            cout<<"8841716993739701954543616000000";
            return 0;
        }
        for(int i = 1; i <= n; i++)ans = ans*i;
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    sqlserver tips
    mysql tips
    小知识点集锦
    设计模式
    将微博或者qq空间的说说同步至博客园 wcf+js(ajax)跨域请求(1)
    WCF服务寄宿IIS与Windows服务
    C# 基础小知识之yield 关键字
    WPF命令绑定 自定义命令
    KnockOut 绑定之foreach绑定(mvc+knockout)
    P5019 铺设道路
  • 原文地址:https://www.cnblogs.com/gwj1314/p/9444710.html
Copyright © 2011-2022 走看看