#include<stdio.h>
int main () {
int n,x[100000] = {0},y[100000] = {0},tem;
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%d %d",&x[i],&y[i]);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - i - 1; j++) {
if(x[j] > x[j+1]) { //先對x做排序
tem = x[j+1];
x[j+1] = x[j];
x[j] = tem;
tem = y[j+1];
y[j+1] = y[j];
y[j] = tem;
}
if(x[j] == x[j+1] && y[j] > y[j+1]) { //x和x+1相同的情況下再對y做排序
tem = y[j+1];
y[j+1] = y[j];
y[j] = tem;
tem = x[j+1];
x[j+1] = x[j];
x[j] = tem;
}
}
}
for(int i = 0; i < n; i++) {
printf("%d %d\n",x[i],y[i]);
}
return 0;
}
#include<stdio.h>
struct s {
int x;
int y;
}p[100000];
void swap(int *x,int *y) {
int tem;
tem = *x;
*x = *y;
*y = tem;
}
int main () {
int n,tem;
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%d %d",&p[i].x,&p[i].y);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - i - 1; j++) {
if(p[j].x > p[j+1].x) { //先對x做排序
swap(&p[j].x,&p[j+1].x);
swap(&p[j].y,&p[j+1].y);
}
if(p[j].x == p[j+1].x && p[j].y > p[j+1].y) { //x和x+1相同的情況下再對y做排序
swap(&p[j].x,&p[j+1].x);
swap(&p[j].y,&p[j+1].y);
}
}
}
for(int i = 0; i < n; i++) {
printf("%d %d\n",p[i].x,p[i].y);
}
return 0;
}
利用結構struct取代array跟指標swap交換副函數,可以減少重複的程式~