六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

STL之multiset应用。

[复制链接]

升级  93.8%

309

主题

309

主题

309

主题

进士

Rank: 4

积分
969
 楼主| 发表于 2013-1-26 13:37:58 | 显示全部楼层 |阅读模式
要求:可添加数,可删除最大、最小的数,可查询当前最大、最小的数。
思路:multiset里可以有重复的数,但删除的时候,会把相同的数一并删除,只需将多删的数再加进去即可。
#include<iostream>#include<cstring>#include<cstdio>#include<set>using namespace std;  int main(){   int a,b;int c,d;   int i=0;   char ch[4];   scanf("%d%d",&a,&b);   getchar();   multiset <int> tree;   tree.clear ();   multiset<int >::iterator  it,pre;   tree.insert (b);   while(i<a)   {   scanf("%s",ch);   if(!strcmp(ch,"A"))   {      scanf("%d",&c);  getchar();  tree.insert(c);   }   else   if(!strcmp(ch,"DI")){    int temp=*(tree.begin());  int m=tree.count (temp);  tree.erase (temp);  for(int j=1;j<m;j++)  tree.insert (temp);   }else  if(!strcmp(ch,"DA")){int temp=*(--tree.end ());int m=tree.count (temp);tree.erase(temp);for(int j=1;j<m;j++)tree.insert (temp);}else if(!strcmp(ch,"Q")){   printf("%d %d\n",*(tree.begin ()),*(--tree.end ()));}i++;   }   return 0;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表