您的位置:首页 > 其它

三级排序

2016-04-29 20:46 253 查看
三级排序

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 363Accepted: 177
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

【题目链接】

点点点



【题目分析】
做这个题时我是通过分类中STL来找的,如果只想做出来可以定义二维数组来 做会好懂点,这里就不详细说明了,都应该会,下面接着我用STL做的分析吧,这 到题我最初用的string对象作为map元素来做的,后来通过提交和验证,发现竟然 不行,空格的ASCII竟然比数字的ASCII大,所以有不相等位的时候就会错,之后就
不知道怎么做了,不过看了一下书,想起了用map键值两变量来做,做了一半想起 输入时如果相同的x和y坐标相等不就覆盖了,试试定义三个吧,想到可以用映照数 据++来做,输出时用一个while()就可以了,谁知道这都可以这样一做加深了我对 map和重载字符的了解,还是很有收获的



【代码实现】
<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>


自己探索着,做出来的,有什么不对,或有更简单的方法记得告诉我。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: