c语言结构体给歌手分数排序

``````#include<stdio.h>
#include <stdlib.h>

typedef struct result
{
int num[10];
int score[10];

}result;

void sorting(result *p)
{
int i,j,k,temp;
for(i = 0;i <9;i++)
{
k = i;
for(j = i+1;j<10;j++)
{
if(p->score[i] < p->score[j])
{
k = j;
}
}
if(k != i)
{
temp = p->score[j];
p->score[j] = p->score[i];
p->score[i] = temp;
}
}
}

int main()
{
int i;
result people;
printf("input:\n");
for(i = 0;i <10;i++)
{
scanf("%d %d",&people.num[i],&people.score[i]);
}
sorting(&people.score);
for(i = 0;i < 10; i++)
{
printf(" %d %d\n",i,people.score[i]);
}

}

``````

2个回答

``````#include <stdio.h>
#include <stdlib.h>

typedef struct result
{
int num[10];
int score[10];

}result;

void sorting(result *p)
{
int i, j, k, temp;
for (i = 0; i < 9; i++)
{
k = i;
for (j = i + 1; j<10; j++)
{
if (p->score[k] < p->score[j])
{
k = j;
}
}
if (k != i)
{
temp = p->score[k];
int t1 = p->num[k];
p->score[k] = p->score[i];
p->num[k] = p->num[i];
p->score[i] = temp;
p->num[i] = t1;
}
}
}

int main()
{
int i;
result people;
printf("input:\n");
for (i = 0; i <10; i++)
{
scanf("%d %d", &people.num[i], &people.score[i]);
}
sorting(&people);
for (i = 0; i < 10; i++)
{
printf(" %d %d\n", people.num[i], people.score[i]);
}
}

``````

input:
1 1
2 4
3 2
4 5
5 1
6 7
7 1
8 5
9 9
10 1
9 9
6 7
4 5
8 5
2 4
3 2
7 1
5 1
1 1
10 1
Press any key to continue . . .