2013
12-13

# 九度-1196-成绩排序[解题代码]

3
1 90
2 87
3 92

2 87
1 90
3 92

cpp 代码如下：
#include<stdio.h>

int main(int argc, char *argv[])
{
int n ;

while(scanf("%d",&n) != EOF ){
int *p = new int[n];
int *q = new int[n];
for(int i=0; i<n; i++){
scanf("%d %d",&p[i],&q[i]);
//cin>>p[i]>>q[i];
}

for(int i=n; i>0; i--){
int flag = 1;
for(int j=0; j<i-1; j++){
if(p[j] > p[j+1]){
flag = 0;
int temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
temp = q[j];
q[j] = q[j+1];
q[j+1] = temp;
}
}
if(flag)
break;			}
for(int i=n; i>0; i--){
int flag = 1;
for(int j=0; j<i-1; j++){
if(q[j] > q[j+1]){
flag = 0;
int temp = q[j];
q[j] = q[j+1];
q[j+1] = temp;
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
if(flag)
break;
}
for(int i=0; i<n; i++){
printf("%d %d\n", p[i], q[i]);

}
}

return 0;
}

/**************************************************************
Problem: 1196
User: coder
Language: C++
Result: Accepted
Time:50 ms
Memory:1412 kb
****************************************************************/

1. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。