三级排序
2016-04-29 20:46
253 查看
三级排序
Description
给你三维坐标(x, y, z),请给三维坐标排序,优先按x值从小到大排序,当x值一样时,按y值从小到大排序,当y值一样时,按z值从小到大排序。
Input
输入数据的第一行是一个数据T,表示有T组数据。
对于每组数据先输入n (1 < n < 50000),表示有n个三维坐标,然后是n行,每行输入三个整数x, y, z。
Output
对于每组输入数据,先输出n,然后输出n行,为排序后的坐标。
Sample Input
2
2
1 2 3
3 2 1
4
2 3 4
2 2 3
2 3 2
3 4 1
Sample Output
2
1 2 3
3 2 1
4
2 2 3
2 3 2
2 3 4
3 4 1
【题目链接】
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
点点点
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
【题目分析】
做这个题时我是通过分类中STL来找的,如果只想做出来可以定义二维数组来 做会好懂点,这里就不详细说明了,都应该会,下面接着我用STL做的分析吧,这 到题我最初用的string对象作为map元素来做的,后来通过提交和验证,发现竟然 不行,空格的ASCII竟然比数字的ASCII大,所以有不相等位的时候就会错,之后就
不知道怎么做了,不过看了一下书,想起了用map键值两变量来做,做了一半想起 输入时如果相同的x和y坐标相等不就覆盖了,试试定义三个吧,想到可以用映照数 据++来做,输出时用一个while()就可以了,谁知道这都可以这样一做加深了我对 map和重载字符的了解,还是很有收获的
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
。
【代码实现】
自己探索着,做出来的,有什么不对,或有更简单的方法记得告诉我。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 363 | Accepted: 177 |
给你三维坐标(x, y, z),请给三维坐标排序,优先按x值从小到大排序,当x值一样时,按y值从小到大排序,当y值一样时,按z值从小到大排序。
Input
输入数据的第一行是一个数据T,表示有T组数据。
对于每组数据先输入n (1 < n < 50000),表示有n个三维坐标,然后是n行,每行输入三个整数x, y, z。
Output
对于每组输入数据,先输出n,然后输出n行,为排序后的坐标。
Sample Input
2
2
1 2 3
3 2 1
4
2 3 4
2 2 3
2 3 2
3 4 1
Sample Output
2
1 2 3
3 2 1
4
2 2 3
2 3 2
2 3 4
3 4 1
【题目链接】
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
点点点
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif)
【题目分析】
做这个题时我是通过分类中STL来找的,如果只想做出来可以定义二维数组来 做会好懂点,这里就不详细说明了,都应该会,下面接着我用STL做的分析吧,这 到题我最初用的string对象作为map元素来做的,后来通过提交和验证,发现竟然 不行,空格的ASCII竟然比数字的ASCII大,所以有不相等位的时候就会错,之后就
不知道怎么做了,不过看了一下书,想起了用map键值两变量来做,做了一半想起 输入时如果相同的x和y坐标相等不就覆盖了,试试定义三个吧,想到可以用映照数 据++来做,输出时用一个while()就可以了,谁知道这都可以这样一做加深了我对 map和重载字符的了解,还是很有收获的
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
。
【代码实现】
<span style="color:#000099;">#include<string> #include<map> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct Info { int ax; int by; int cz; bool operator < (const Info &a) const // 重载<运算符 { if(a.ax!=ax) // 比较x坐标 return a.ax>ax; else if(a.by!=by) // 如果x相等比较y坐标 return a.by>by; else // 如果前面都相等比较z坐标 return a.cz>cz; } }; int main() { string s; map<Info,int>m; // 定义属于自己的容器 int t; cin>>t; // 输人t组数据 while(t--) { int n,i,a,b,c; cin>>n; Info info; // 定义结构体变量 getchar(); map<Info,int>::iterator it; for(i=0;i<n;i++) { cin>>a>>b>>c; // 输出坐标 info.ax=a; info.by=b; info.cz=c; m[info]++; // 插入到map容器中 } cout<<n<<endl; for(it=m.begin();it!=m.end();it++) { while((*it).second!=0) // 如果输入的坐标重复,就多次输出 { </span><strong style="font-family: serif; font-size: 12px; background-color: rgb(255, 255, 255);"><span style="font-size:24px;"></span></strong><pre name="code" class="cpp" style="display: inline !important;"><span style="color:#000099;">cout<<((*it).first).ax<<" "<<((*it).first).by<<" "<<((*it).first).cz<<endl;</span>
<span style="color:#000099;"> (*it).second--; } } m.clear(); } return 0; }</span>
自己探索着,做出来的,有什么不对,或有更简单的方法记得告诉我。
相关文章推荐
- iOS 硬件 定位 - 详解 数据的处理
- linux先进程通信 管道方式和消息队列
- 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)
- PHP经典算法过桥。
- 输入框和div拖动
- Linux ssh密钥自动登录 专题
- iOS 硬件
- iOS 硬件 定位 - 前台
- 如何设置文本文件的默认保存编码为UTF-8
- 4月末开坑,4.29-?
- JAVA EE的学习路线
- HDU 1242 Rescue(BFS+【优先队列】)
- iOS上架ipa上传问题那些事
- 在文本框中输入一个大于零的数字
- iOS 硬件 定位 - 前后台
- linux PHP卸载不了
- matlab的基本命令·画图篇
- 订单捕获-销售订单概览
- 基础卷_异常篇 第10集 异常--总结
- 最长上升子序列的个数