zoukankan      html  css  js  c++  java
  • poly

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    struct term{//单项式
        ll index;
        double coeff;
        term(double coeff_=0,ll index_=0):coeff(coeff_),index(index_) {}
    };
    istream& operator >> (istream& in,term& t){//读入单项式
        in>>t.coeff>>t.index;
        return in;
    }
    ostream& operator << (ostream& out,const term& t){//输出单项式(有前缀 '+' )
        out<<(t.coeff>0?'+':'-');
        if(t.index==0){//常数直接输出
            out<<fabs(t.coeff);
            return out;
        }
        if( fabs(fabs(t.coeff)-1)>=1e-6 ) out<<fabs(t.coeff);// +-1x^n 输出为 +-x^n
        out<<'x';
        if(t.index>1) out<<'^'<<t.index;//大于 1 次的才要输出次数
        return out;
    }
    term operator - (const term& a) { return term(-a.coeff,a.index); }//系数为相反数的单项式
    term operator + (const term& a,const term& b) { return term(a.coeff+b.coeff,a.index); }//单项式的加法
    term operator * (const term& a,const term& b) { return term(a.coeff*b.coeff,a.index+b.index); }//单项式的乘法
    bool operator < (const term& a,const term& b) { return a.index<b.index; }//单项式次数是否更小
    
    struct poly{//多项式
        term terms[262144];
        int count;
        poly() { count=0; }
    };
    inline void arrange(poly& p){//整理多项式
        sort(p.terms+1,p.terms+1+p.count);
        int cur=1;
        for(int i=2;i<=p.count;i++)//合并次数相同的项
            if(p.terms[cur].index!=p.terms[i].index) p.terms[++cur]=p.terms[i];
            else p.terms[cur]=p.terms[cur]+p.terms[i];
        p.count=cur;
    
        cur=0;
        for(int i=1;i<=p.count;i++)//删除系数为 0 的项
            if(fabs(p.terms[i].coeff)>1e-6) 
                p.terms[++cur]=p.terms[i];
        p.count=cur;
    
        if(p.count==0) p.terms[++p.count]=term(0,0);//删空了,说明为 0
    }
    istream& operator >> (istream& in,poly& p){//读入多项式
        in>>p.count;
        for(int i=1;i<=p.count;i++) in>>p.terms[i];
        arrange(p);
        return in;
    }
    ostream& operator << (ostream& out,const poly& p){//输出多项式
        if(p.terms[p.count].index==0){//只有常数项,直接输出
            out<<p.terms[1].coeff;
            return out;
        }
        
        if(p.terms[p.count].coeff<0) out<<'-';//第一项不要前缀 '+' 单独处理
        if( fabs(fabs(p.terms[p.count].coeff)-1)>=1e-6 ) out<<fabs(p.terms[p.count].coeff);
        out<<'x';
        if(p.terms[p.count].index>1) out<<'^'<<p.terms[p.count].index;
        for(int i=p.count-1;i>=1;i--) out<<p.terms[i];//输出之后每一项
        return out;
    }
    inline void add(poly& pa,const poly& pb){//多项式 pb 累加至多项式 pa
        for(int i=1;i<=pb.count;i++) pa.terms[pa.count+i]=pb.terms[i];
        pa.count+=pb.count;
        arrange(pa);
    }
    inline void dis(poly& pa,const poly& pb){//多项式 pb 累减至多项式 pa
        for(int i=1;i<=pb.count;i++) pa.terms[pa.count+i]=-pb.terms[i];
        pa.count+=pb.count;
        arrange(pa);
    }
    poly tmpPoly;
    inline void mul(poly& pa,const poly& pb){// 多项式 pb 累乘至 pa
        tmpPoly.count=0;//先用 tmpPoly 记录
        for(int i=1;i<=pa.count;i++)
            for(int j=1;j<=pb.count;j++)
                tmpPoly.terms[++tmpPoly.count]=pa.terms[i]*pb.terms[j];
        arrange(tmpPoly);
        pa=tmpPoly;//拷贝回 pa
    }
    
    inline char getOperator(){//获取操作符
        char c=0;
        while(c!='+'&&c!='-'&&c!='*') cin>>c;
        return c;
    }
    
    int K;
    poly Ans,Poly[20];
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cout.precision(6);
    
        cin>>K;
        cin>>Ans;
        for(int i=1;i<K;i++) cin>>Poly[i];
        for(int i=1;i<K;i++){
            char c=getOperator();
            if(c=='+') add(Ans,Poly[i]);
            else if(c=='-') dis(Ans,Poly[i]);
            else if(c=='*') mul(Ans,Poly[i]);
        }
        cout<<Ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    DirectShow音频采集pcm,实时编码AAC,附源码
    live555 RTSP推送到Darwin出现404错误的解决
    live555 RTSP推送到Darwin出现404错误的解决
    公网RTSP地址(持续更新)
    公网RTSP地址(持续更新)
    手机Android音视频采集与直播推送,实现单兵、移动监控类应用
    手机Android音视频采集与直播推送,实现单兵、移动监控类应用
    用Darwin开发RTSP级联服务器(拉模式转发)(附源码)
    用Darwin开发RTSP级联服务器(拉模式转发)(附源码)
    TCP/IP和Socket的关系
  • 原文地址:https://www.cnblogs.com/JustinRochester/p/13681882.html
Copyright © 2011-2022 走看看