ZJU-PAT 1057. Stack (30)---17有3组超时 浙大2013年上机复试第三题

题目:

很弱的代码:

#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;  
}
结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值