求助,怎用优先队列解此题
查看原帖
求助,怎用优先队列解此题
1089734
ym886楼主2023/11/11 22:54
#include<bits/stdc++.h>
using namespace std;
int s[2];//0->*  1->+
priority_queue<int, vector<int>, greater<int> > q;
void f0(){
    while(s[0]){
            s[0]--;
            int a = q.top();
            q.pop();
            int b=q.top();
            q.pop();
            q.push(a*b);
    }
}
void f1(){
    while(s[1]){
            s[1]--;
            int a = q.top();
            q.pop();
            int b=q.top();
            q.pop();
            q.push(a+b);
    }
}
int main(){
    int cnt=0;//存0的数量
    for(int i=0;i<4;i++){
        int tp; cin>>tp;
        if(tp==0) cnt++;
        q.push(tp);
    }
    for(int i=0;i<3;i++){
        char tp; cin>>tp;
        if(tp=='*') s[0]++;
        else s[1]++;
    }
    if(!cnt or !s[0]){
        f0();
        f1();
        cout<<q.top();
    }else if(s[0]==3){
        cout<<0; return 0;
    }else{
        if(cnt==1){
            q.pop();
            int a=q.top(); q.pop();
            int b=q.top(); q.pop();
            if(s[0]==2){
                cout<<a; return 0;
            }else{
                cout<<a+b; return 0;
            }
        }else if(cnt==2){
            if(s[0]==2) cout<<0;
            else{
                q.pop();q.pop(); cout<<q.top();
            }
        }else cout<<0;
    }
    return 0;
}
2023/11/11 22:54
加载中...