nyoj8 一种排序(用set)
来源:互联网 发布:九城软件电话 编辑:程序博客网 时间:2024/05/21 08:38
一种排序
时间限制:3000 ms | 内存限制:65535 KB难度:3- 描述
- 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;- 输入
- 第一行有一个整数 0<n<10000,表示接下来有n组测试数据;每一组第一行有一个整数 0<m<1000,表示有m个长方形;接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,
第二个和第三个数值大的表示长,数值小的表示宽,相等说明这是一个正方形(数据约定长宽与编号都小于10000); - 输出
- 顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
- 样例输入
181 1 11 1 11 2 11 1 21 2 22 2 12 1 12 1 2
- 样例输出
1 1 11 2 12 1 11 2 22 2 1
一种排序
时间限制:3000 ms | 内存限制:65535 KB
难度:3
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);
1.按照编号从小到大排序
2.对于编号相等的长方形,按照长方形的长排序;
3.如果编号和长都相同,按照长方形的宽排序;
4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
- 输入
- 第一行有一个整数 0<n<10000,表示接下来有n组测试数据;每一组第一行有一个整数 0<m<1000,表示有m个长方形;接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,第二个和第三个数值大的表示长,数值小的表示宽,相等说明这是一个正方形(数据约定长宽与编号都小于10000);
- 输出
- 顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
- 样例输入
- 181 1 11 1 11 2 11 1 21 2 22 2 12 1 12 1 2
- 样例输出
- 1 1 11 2 12 1 11 2 22 2 1
#include <iostream>#include <cstdio>#include <set>#include <iterator>using namespace std;struct node { int num, length, width;};bool operator < (const node &a, const node &b) { if(a.num != b.num) return a.num < b.num; if(a.length != b.length) return a.length < b.length; return a.width < b.width;}ostream & operator << (ostream &out, const node &a) { return out << a.num << " " << a.length << " " << a.width;}set<node> s;int main() { int n, m, a, b; node t; scanf("%d", &n); while(n--) { scanf("%d", &m); s.clear(); while(m--) { scanf("%d%d%d", &t.num, &a, &b); t.length = a >= b ? a : b; t.width = a <= b ? a : b; s.insert(t); } //set<node>::iterator i; //for(i = s.begin(); i != s.end(); ++i) { //printf("%d %d %d\n", (*i).num, (*i).length, (*i).width); //} copy(s.begin(), s.end(), ostream_iterator<node>(cout,"\n")); } return 0;}
两种输出方式都可以,也可以在重载 << 时加上换行,这样,ostream_iterator<node>()可以只有一个参数cout,即:
ostream & operator << (ostream &out, const node &a) { return out << a.num << " " << a.length << " " << a.width << endl;}copy(s.begin(), s.end(), ostream_iterator<node>(cout));
关于copy函数的解释:http://blog.csdn.net/jingweihu/article/details/23241269
使用ostream_iterator< >()时,记得引用头文件#include <iterator>当然输入可以通过重载 >>运算符实现,而且可以更快,然而宝宝懒~~
istream& operator >> (istream& in, node &t) { in >> t.num; int a, b; cin >> a >> b; t.length = max(a, b); t.width = min(a, b); return in;}
0 0
- nyoj8 一种排序(用set)
- 一种排序(nyoj8)(简单排序)
- nyoj8 一种排序(sort三级排序)
- nyoj8一种排序
- nyoj8 一种排序
- NYOJ8一种排序
- NYOJ8一种排序
- nyoj8 一种排序(排序变形)
- nyoj8(一种排序)(结构体的二级排序)
- nyoj8
- 一种排序STL在set中的结构体排序
- 一种排序(set集合 结构体的应用)
- 一种排序算法(bitset)
- NYOJ 8 一种排序(comparator排序)
- 一种排序算法(快速排序)
- 一种简单的用java实现快速排序(Quicksort)
- 一种简单的用java实现快速排序(Quicksort)
- 一种排序
- Thinking in Java 第22章 图形化用户界面 【捕获事件】
- servlet或struts得到带域名路径
- Oracle数据库快速查询重复记录的方法;
- 从Eclipse到AndroidStudio(二)界面介绍
- 云图网络书签收藏分类管理
- nyoj8 一种排序(用set)
- java虚拟机类加载机制 ——类加载的过程
- 聊聊进程异常重启的问题------顺便详解linux句柄泄漏问题的定位(知识点: lsof -p xxx; ll /proc/xxx/fd)
- MyEclipse2014中导入com.sun.management.OperatingSystemMXBean出错和中文乱码解决方案
- 给Eclipse提速的7个技巧
- codis 找不到"golang.org/x/tools/go/vcs"
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com
- unity3d计算两物体之间的夹角
- IP层关联协议之ICMP