nyoj 8 一种排序(用vector,sort,不用set)
2017-02-13 11:54
246 查看
一种排序
时间限制: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 <vector> #include <algorithm> #include <cstdio> using namespace std; typedef struct retangle{ int number; int length; int width; }rectangle; bool comp(rectangle a, rectangle b){ if(a.number != b.number) return a.number < b.number; else if(a.length != b.length) return a.length < b.length; else return a.width < b.width; } int main(){ int test, i, n; cin >> test; vector<rectangle> v; rectangle r; while(test--){ v.clear(); cin >> n; for(int j = 0; j < n; j++){ cin >> r.number >> r.length >> r.width; if(r.length < r.width) swap(r.length, r.width); v.push_back(r); } sort(v.begin(), v.end(), comp); //cout << v[0].number << v[0].length << v[0].width << endl; printf("%d %d %d\n", v[0].number, v[0].length, v[0].width); for(i = 1; i < n; i++){ if((v[i].number == v[i-1].number) && (v[i].length == v[i-1].length) && (v[i].width == v[i-1].width)) continue; else printf("%d %d %d\n", v[i].number, v[i].length, v[i].width); //cout << v[i].number << v[i].length << v[i].width << endl; } } //system("pause"); return 0; }
相关文章推荐
- NYoj 8 一种排序(sort/qsort实例)
- nyoj8 一种排序(用set)
- 关于C++中vector和set使用sort方法进行排序
- nyoj 8 一种排序(sort运用)
- NYOJ - 8 - 一种排序(STL - set)
- nyoj8 一种排序(sort三级排序)
- NYOJ 4 一种排序 (sort)
- NYOJ-8 一种排序【sort与qsort】
- NYOJ 8 一种排序
- NYOJ 8 一种排序 (结构体排序)
- nyoj 8 一种排序
- NYOJ 8 一种排序 快排 去重
- NYOJ-8 一种排序
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- nyoj 8 一种排序
- std list/vector sort 自定义类的排序
- 【LeetCode41-50】找出丢失最小正整数,计算蓄水量,检测?*匹配(44),跳跳棋,全排列,矩阵逆转,颠倒字母重新排序(auto,set/multiset,vector初始化),n次方
- 【Redis缓存机制】7.SortSet排序集合类型操作
- Vector sort 排序
- NYOJ 8 一种排序