题目:
很弱的代码:
#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 100000
int ans[N];
int tmp[N];
bool cmp(int a,int b){
return a<b;
}
int main()
{
int n,i,j,top,mid;
string str;
//freopen("1057.txt","r",stdin);
//freopen("1057_w.txt","w",stdout);
while(scanf("%d",&n)!=EOF){
top=0;
for(i=0;i<n;i++){
cin>>str;
if(str=="Pop"){
if(top){
cout<<ans[top]<<endl;
top--;
}
else
cout<<"Invalid"<<endl;
}
else if(str=="PeekMedian"){
if(top){
if(top%2==0)
mid=top/2;
else
mid=(top+1)/2;
for(j=1;j<=top;j++){
tmp[j]=ans[j];
}
sort(tmp+1,tmp+1+top,cmp);
cout<<tmp[mid]<<endl;
}
else
cout<<"Invalid"<<endl;
}
else if(str=="Push"){
top++;
cin>>ans[top];
}
}
}
return 0;
}
结果: