题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <stdio.h>
#define N 1000
struct SC {
char name[N];
int score;
};
int main() {
int n;
while(scanf("%d", &n)!=EOF){
struct SC s1[N];
int flag;
scanf("%d", &flag);
for (int i = 0; i<n; i++) {
scanf("%s", s1[i].name);
scanf("%d", &s1[i].score);
}
if (flag == 0) {//降序,从大到小
// printf("1");
for (int i = 0; i<n; i++) {
// printf("2");
for (int j = 1; j < n - i;j++) {
// printf("3");
// printf("%d %d %d %d \n",i,j,s1[i].score , s1[j].score);
if (s1[j-1].score < s1[j].score) {
// printf(" 5 \n");
struct SC temp;
temp= s1[j-1];
s1[j-1] = s1[j];
s1[j] = temp;
}
}
}
for (int i = 0; i < n;i++) {
// printf("5");
printf("%s %d\n", s1[i].name, s1[i].score);
}
} else if (flag == 1) {//升序 从小到大
for (int i = 0; i < n;i++) {
for (int j = 1; j < n - i; j++) {
// printf("%d %d %d %d \n",i,j,s1[i].score , s1[j].score);
if (s1[j-1].score > s1[j].score) {
// printf(" 5 \n");
struct SC temp;
temp= s1[j-1];
s1[j-1] = s1[j];
s1[j] = temp;
}
}
}
for (int i = 0; i < n;i++) {
printf("%s %d\n", s1[i].name, s1[i].score);
}
}
}
return 0;
}
很怪~

安克创新 Anker公司福利 716人发布