10-排序5 PAT Judge

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

typedef struct stu{
	int id;
	int s[6];//各题分数
	int score;//总分
	int pass;//完全正确的题目数
	int ns;//无提交或无任何通过编译标志,0代表没有通过
} Stu;

void sort(Stu stu[], int N);
int compare(const Stu *s1, const Stu *s2);

int main(int argc, char const *argv[])
{
	// freopen("test.txt", "r", stdin);
	int N, K, M;
	scanf("%d %d %d", &N, &K, &M);
	Stu *student = (Stu*)malloc(sizeof(Stu)*(N+1));
	for (int i = 1; i <= N; i++){
		student[i].id = 0, student[i].score = 0, student[i].pass = 0, student[i].ns = 0;
		for (int j = 1; j <= K; j++)
			student[i].s[j] = -2;
	}
	int p[6];//各题满分
	for (int i = 1; i <= K; i++)
		scanf("%d", &p[i]);
	int id, p_id, ps;
	for (int i = 0; i < M; i++){
		scanf("%d %d %d", &id, &p_id, &ps);
		if (ps > student[id].s[p_id])
			student[id].s[p_id] = ps;
	}
	for (int i = 1; i <= N; i++){
		student[i].id = i;
		for (int j = 1; j <= K; j++){
			if (student[i].s[j] >= 0){
				student[i].score += student[i].s[j];
				student[i].ns = 1;
			}
			if (student[i].s[j] == p[j])
				student[i].pass++;
		}
	}
	sort(student, N);
	int rank = 1;
	int score = student[1].score;
	for (int i = 1; i <= N; i++){
		if (student[i].ns == 0)
			// break;	//break不应该退出了整个for循环吗,为什么结果都过了
			continue;	//我没想明白这里为什么用break和continue对结果无影响,都过了
		if (student[i].score != score){
			rank = i;
			score = student[i].score;
		}
		printf("%d %05d %d", rank, student[i].id, student[i].score);
		for (int j = 1; j <= K; j++){
			if (student[i].s[j] >= 0)
				printf(" %d", student[i].s[j]);
			else if (student[i].s[j] == -1)
				printf(" 0");
			else
				printf(" -");
		}
		printf("\n");
	}
	free(student);
	return 0;
}

void sort(Stu student[], int N)
{
	Stu *tmp = (Stu*)malloc(sizeof(Stu));
	for (int P = 2; P <= N; P++){
		*tmp = student[P];
		int i;
		for (i = P; i > 1 && compare(&student[i-1], tmp); i--)
			student[i] = student[i-1];
		student[i] = *tmp;
	}
	free(tmp);
}

int compare(const Stu *s1, const Stu *s2)
{
	if (s1->score < s2->score)
		return 1;
	else if (s1->score == s2->score)
		return s1->pass < s2->pass;
	else
		return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值