优先队列 的实例(未完----待续)

                     一种排序

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);

1.按照编号从小到大排序

2.对于编号相等的长方形,按照长方形的长排序;

3.如果编号和长都相同,按照长方形的宽排序;

4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
 
输入
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,

第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1


#include<iostream>
#include<queue>
#include<vector>
using namespace std;
class squar
{
int NO;
int lenth;
int width;
public:
void SetNO(){cin>>NO;}
void SetLenth(){cin>>lenth;}
void SetWidth(){cin>>width;}
int GetNO() const{return NO;}
int GetLenth() const{return lenth;}
int GetWidth() const{return width;}
bool operator<(const squar& s)const
{
int NO1=s.GetNO();
int lenth1=s.GetLenth();
int width1=s.GetWidth();
if(NO>NO1) return true;
if((NO==NO1)&&(lenth>lenth1)) return true;
if((NO==NO1)&&(lenth==lenth1)&&(width>width1)) return true;
//if((NO==NO1)&&(lenth==lenth1)&&(width==width1))
return false;
}
};
int main()
{
squar s[100];
int n,i=0;
cin>>n;
while(i<n)
{
//初始化该数组
s[i].SetNO();
s[i].SetLenth();
s[i].SetWidth();
i++;
}
priority_queue<squar>pr(s,s+n); //降序排序成功了,但是升序还是不行,priority_queue<squar,vector,greater<squar>>pr(s,s+n);
while(!pr.empty())
{
squar& t=pr.top();

cout<<t.GetNO()<<"\t"<<t.GetLenth()<<"\t"<<t.GetWidth()<<endl;
pr.pop();
}
return 0;
}

 

 

 

//这题没有实现完全,还有一个功能没有实现:即不能去掉重复的元素————————————————————————

待续



posted @ 2014-03-29 10:15  zhoudan  阅读(154)  评论(0编辑  收藏  举报