P4940 20pts 求助
  • 板块学术版
  • 楼主_Weslie_
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/10/4 14:28
  • 上次更新2023/11/2 15:50:26
查看原帖
P4940 20pts 求助
511959
_Weslie_楼主2023/10/4 14:28

有两份都是 20pts(一份我的,一份我同学的)

玄关

我的:

#include<iostream>
#include<stack>
#define ac "SUCCESS\n"
#define int long long
#define wa "UNSUCCESS\n"
using namespace std;
int f[2]= {0,1};
stack<int>st[2];
string s;
int i,m,j;
int abss(int x) {
	if(x<0)return -x;
	return x;
}
signed main() {
	while(cin>>s) {
		if(s=="PUSH") {
			cin>>i>>m;
			printf(ac);
			st[f[i]].push(m);
		}
		if(s=="POP") {
			cin>>i;
			if(!st[f[i]].empty()) {
				printf(ac);
				st[f[i]].pop();
			} else printf(wa);
		}
		if(s=="ADD") {
			cin>>i;
			if(!st[1].empty()&&!st[0].empty()) {
				printf(ac);
				int x=st[1].top()+st[0].top();
				st[1].pop();
				st[0].pop();
				st[f[i]].push(x);
			} else printf(wa);
		}
		if(s=="SUB") {
			cin>>i;
			if(!st[1].empty()&&!st[0].empty()) {
				printf(ac);
				int x=abss(st[1].top()-st[0].top());
				st[1].pop();
				st[0].pop();
				st[f[i]].push(x);
			} else printf(wa);
		}
		if(s=="DEL") {
			cin>>i;
			printf(ac);
			while(!st[f[i]].empty())st[f[i]].pop();
		}
		if(s=="MOVE") {
			cin>>i>>j;
			printf(ac);
			if(st[f[i]].size()>=st[f[j]].size()) {
				while(!st[f[j]].empty()) {
					st[f[i]].push(st[f[j]].top());
					st[f[j]].pop();
				}
			}
			else{
				swap(f[i],f[j]);
				while(!st[f[j]].empty()) {
					st[f[i]].push(st[f[j]].top());
					st[f[j]].pop();
				}
			}
		}
		if(s=="SWAP"){
			printf(ac);
			swap(f[0],f[1]);
		}
		if(s=="END"){
			printf(ac);
			if(st[f[0]].empty())printf("NONE\n");
			else while(!st[f[0]].empty()){
				printf("%d ",st[f[0]].top());st[f[0]].pop();
			}
			printf("\n");
			swap(f[0],f[1]);
			if(st[f[0]].empty())printf("NONE\n");
			else while(!st[f[0]].empty()){
				printf("%d ",st[f[0]].top());st[f[0]].pop();
			}
		}
	}
	return 0;
}

我同学的:

#include <bits/stdc++.h>
#define suc cout << "SUCCESS\n";
#define usc cout << "UNSUCCESS\n";
#define int long long
using namespace std;
signed main() {
    stack<int> stk[2];
    int id[2] = {0, 1};
    string s;
    while (cin >> s) {
        if (s == "PUSH") {
            int p, x;
            cin >> p >> x;
            p = id[p];
            suc;
            stk[p].push(x);//done
        } else if (s == "POP") {
            int p;
            cin >> p;
            p = id[p];
            if (stk[p].size()) {
                suc;
                stk[p].pop();
            } else {
                usc;
            }//done
        } else if (s == "ADD") {
            int x;
            cin >> x;
            if (stk[0].size() && stk[1].size()) {
                suc;
                int v = stk[0].top(), w = stk[1].top();
                stk[0].pop(), stk[1].pop();
                stk[id[x]].push(v + w);
            } else {
                usc;
            }//done
        } else if (s == "SUB") {
            int x;
            cin >> x;
            if (stk[0].size() && stk[1].size()) {
                suc;
                int v = stk[0].top(), w = stk[1].top();
                stk[0].pop(), stk[1].pop();
                stk[id[x]].push(abs(v - w));
            } else {
                usc;
            }//done
        } else if (s == "DEL") {
            int x;
            cin >> x;
            x = id[x];
            suc;
            while (stk[x].size()) {
                stk[x].pop();
            }//done
        } else if (s == "MOVE") {
            int x, y;
            cin >> x >> y;
            x = id[x], y = id[y];
            suc;
            if (stk[x].size() > stk[y].size()) {
                while (stk[y].size()) {
                    stk[x].push(stk[y].top());
                    stk[y].pop();
                }
            } else {
                while (stk[x].size()) {
                    stk[y].push(stk[x].size());
                    stk[x].pop();
                }
                swap(id[0], id[1]);
            }
        } else if (s == "SWAP") {
            suc;
            swap(id[0], id[1]);
        } else {
            suc;
            if (stk[id[0]].size()) {
                while (stk[id[0]].size()) {
                    cout << stk[id[0]].top() << ' ';
                    stk[id[0]].pop();
                }
                cout << '\n';
            } else {
                cout << "NONE\n";
            }
            if (stk[id[1]].size()) {
                while (stk[id[1]].size()) {
                    cout << stk[id[1]].top() << ' ';
                    stk[id[1]].pop();
                }
                cout << '\n';
            } else {
                cout << "NONE\n";
            }
            break;
        }
    }
    return 0;
}
2023/10/4 14:28
加载中...