话不多说,直接贴代码
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct student //定义结构体
{
char name[20];
int t;
int allscore;
};
bool cmp(struct student c, struct student b) //定义比较函数,按照降序排列
{
if (c.allscore > b.allscore) //如果改为小于符号,那就是升序排列
{
return true;
}
return false;
}
int main()
{
struct student s[1000];
int n, m, line, score[10], sum = 0, sumscore[1000], x = 0;
scanf("%d %d %d\n", &n, &m, &line);
for (int i = 0; i < m; i++)
{
scanf("%d ", &score[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%s %d", &s[i].name, &s[i].t);
for (int j = 0; j < s[i].t; j++)
{
scanf("%d", &x);
sumscore[i] += score[x - 1];
}
s[i].allscore = sumscore[i];
if (sumscore[i] >= line)
sum++;
}
sort(s, s + n - 1, cmp); //对结构体排序就这一句,上面的都是都数据的输入,和对应本题的一些操作,不得不说,sort函数真的好用
printf("%d\n", sum);
sum = 0;
for (int i = 0; i < n; i++)
{
if (s[i].allscore >= line)
{
printf("%s %d\n", s[i].name, s[i].allscore);
}
}
system("pause");
return 0;
}
输出
这个题目呢不光是排序,还有一些求和和比较大小的要求题目如下