记录PTA甲级以及C++部分语法知识1057

30分的题果然没那么简单,自己写超时三个。。。但还是记录一下啊:

大神的归纳:https://blog.csdn.net/sinat_29278271/article/details/47291659

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> st;
vector<int> temp;

int main(){
	int N,key,i,j;
	char com[11];
	string str;
	scanf("%d",&N);
	for(i=0;i<N;i++){
		scanf("%s",com);
		str=com;
		if(str=="Pop"){
			if(st.size()==0)
				printf("Invalid\n");
			else{
				printf("%d\n",st[st.size()-1]);
				st.pop_back();
			}
		}
		else if(str=="Push"){
			scanf("%d",&key);
			st.push_back(key);
			//printf("%d\n",key);
		}
		else if(str=="PeekMedian"){
			if(st.size()==0)
				printf("Invalid\n");
			else{
				temp.clear();
				for(j=0;j<st.size();j++)
					temp.push_back(st[j]);

				sort(temp.begin(),temp.end());
				if(temp.size()%2)
					key=(temp.size()+1)/2-1;
				else key=temp.size()/2-1;
				printf("%d\n",temp[key]);
				//st.pop_back();
			}
		}
	}
		system("pause");
		return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值