按横纵坐标排序
内存限制:256 MiB时间限制:1000 ms输入文件: zuo.in输出文件: zuo.out
题目描述
平面上有 n 个点,坐标均为整数。
先按横坐标从小到大排序;如果横坐标相同,再按纵坐标从小到大排序。
请输出按上述规则排序的结果。
输入格式
第一行是整数n (1<=n<=100),
接下来有 n 行,每行两个整数,表示平面上一个点的坐标。
输出格式
输出有 n 行,即排序后的点,格式为横坐标、纵坐标。
样例
样例输入
4
1 3
2 5
1 4
4 1
样例输出
1 3
1 4
2 5
4 1
C++代码
#include <bits/stdc++.h>
using namespace std;
struct xs {
int xm;
int cj;
};
bool cmp(xs x, xs y) {
if (x.xm<y.xm || x.xm==y.xm&&x.cj<y.cj) {
return true;
}
else
{
return false;
}
}
int main()
{
freopen("zuo.in","r",stdin);
freopen("zuo.out","w",stdout);
int n;
cin>>n;
xs a[1000];
for (int i=0;i<=n-1;i++)
{
cin>>a[i].xm>>a[i].cj;
}
sort(a+0,a+n,cmp);
for(int i=0;i<=n-1;i++)
{
cout<<a[i].xm<<' '<<a[i].cj<<endl;
}
return 0;
}
没登录的看这里
#include <bits/stdc++.h>
using namespace std;
struct xs {
int xm;
int cj;
};
bool cmp(xs x, xs y) {
if (x.xm<y.xm || x.xm==y.xm&&x.cj<y.cj) {
return true;
}
else
{
return false;
}
}
int main()
{
freopen("zuo.in","r",stdin);
freopen("zuo.out","w",stdout);
int n;
cin>>n;
xs a[1000];
for (int i=0;i<=n-1;i++)
{
cin>>a[i].xm>>a[i].cj;
}
sort(a+0,a+n,cmp);
for(int i=0;i<=n-1;i++)
{
cout<<a[i].xm<<' '<<a[i].cj<<endl;
}
return 0;
}