-
Notifications
You must be signed in to change notification settings - Fork 378
/
选美比赛.c
97 lines (87 loc) · 2.11 KB
/
选美比赛.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
用c语言编写软件完成以下任务:一批选手参加比赛,比赛的规则是最后得分越高,名次越低。当半决赛结束时,
要在现场按照选手的出场顺序宣布最后得分和最后名次,获得相同分数的选手具有相同的名次,名次连续编号,
不用考虑同名次的选手人数。例如:
选手序号: 1,2,3,4,5,6,7
选手得分: 5,3,4,7,3,5,6
输出名次为:3,1,2,5,1,3,4
*/
#include <stdio.h>
struct player{
int num;
int score;
int rand;
};
void sortScore(struct player psn[], int n)
{
int i, j;
struct player tmp;
for(i=0; i<n-1; i++)
for(j=0; j<n-1-i; j++)
{
if(psn[j].score>psn[j+1].score)
{
tmp = psn[j];
psn[j] = psn[j+1];
psn[j+1] = tmp;
}
}
}
void setRand(struct player psn[], int n)
{
int i,j=2;
psn[0].rand=1;
for(i=1; i<n; i++)
{
if(psn[i].score!=psn[i-1].score )
{
psn[i].rand=j;
j++;
}
else
psn[i].rand=psn[i-1].rand;
}
}
void sortNum(struct player psn[], int n)
{
int i,j;
struct player tmp;
for(i=0; i<n-1; i++)
for(j=0; j<n-1-i; j++)
{
if(psn[j].num>psn[j+1].num)
{
tmp = psn[j];
psn[j] = psn[j+1];
psn[j+1] = tmp;
}
}
}
void sortRand(struct player psn[], int n)
{
sortScore(psn,n); /*以分数为关键字排序*/
setRand(psn,n); /*按照分数排名次*/
sortNum(psn,n); /*按照序号重新排序*/
}
int main()
{
struct player psn[7]={{1, 5, 0}, {2, 3, 0}, {3, 4, 0}, {4, 7, 0}, {5, 3, 0}, {6, 5, 0}, {7, 6, 0}};
int i;
sortRand(psn, 7);
printf("num score rand \n");
for(i=0; i<7; i++)
{
printf("%d%6d%6d\n", psn[i].num, psn[i].score, psn[i].rand);
}
getche();
return 0;
}
//运行结果:
//num score rand
//1 5 3
//2 3 1
//3 4 2
//4 7 5
//5 3 1
//6 5 3
//7 6 4