原题是要输入九个考试人的信息,排序后输出,
在测试排序时至少要敲162次键盘才可将这些数字输入,很麻烦
所以在同目录下写了一个data.txt
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
用文件输入,测试排序结果变得很轻松,以下为测试代码
源代码在
https://blog.csdn.net/AFANTI_JK/article/details/104448109
#include <stdio.h>
#include <stdlib.h>
typedef struct info{
int score;
unsigned long long int ID;
int f_rank;
int loc;
int l_rank;
}info;
int sort_loc(struct info a[], int left, int right){
int MAX, LAST;
int rank = 1;
int i = left, j;
int n = right - left;
MAX = left;
LAST = -1;
for(j = n; j > 0; --j){
for(i = left; i < right; ++i){
if(a[i].l_rank == 0 && a[i].score > a[MAX].score)
MAX = i;
}
if(a[MAX].score != a[LAST].score || LAST == -1){
a[MAX].l_rank = rank++;
LAST = MAX;
}else{
a[MAX].l_rank = a[LAST].l_rank;
++rank;
}
i = left;
while(a[i].l_rank != 0 && i < right)
++i;
MAX = i;
}
return 0;
}
int sort_all(struct info a[], int sum){
int MAX = 0, LAST = -1;
int rank = 1;
int j;
for(int i = 0; i < sum; ++i){
for(j = 0; j < sum; ++j){
if(a[j].f_rank == 0 && a[j].score > a[MAX].score)
MAX = j;
}
if(LAST == -1 || a[MAX].score != a[LAST].score){
a[MAX].f_rank = rank++;
LAST = MAX;
printf("%14lld %3d %3d\n", a[MAX].ID, a[MAX].f_rank, a[MAX].l_rank);
}else{
a[MAX].f_rank = a[LAST].f_rank;
++rank;
printf("%14lld %3d %3d\n", a[MAX].ID, a[MAX].f_rank, a[MAX].l_rank);
}
j = 0;
while(a[j].f_rank != 0 && j < i)
++j;
MAX = j;
}
}
int main(){
struct info a[30000] = {0};
FILE *f_read = fopen("data.txt", "r");
if(f_read == NULL)
return 0;
for(int i = 0; i < 9; ++i){
fscanf(f_read, "%lld %d", &a[i].ID, &a[i].score);
}
sort_loc(a, 0, 5);
sort_loc(a, 5, 9);
sort_all(a, 9);
return 0;
}
昨晚我梦见你帮我拿了什么东西,然后我把你手提的袋子接过来,你说不重,我感觉其实挺重的,然后你把手放在我提袋子的手下面,一块提着袋子,对我笑眯眯,我感觉很暖心
梦和现实一样,很美好,但也很快,留下影子般痕迹,但确实发生过