梦一场乀


私信TA

用户名:ADream

访问量:35287

签 名:

梦开始的地方。

等  级
排  名 54
经  验 10777
参赛次数 2
文章发表 35
年  龄 21
在职情况 学生
学  校
专  业 软件工程

  自我简介:


解题思路:


        主要用到atoi()函数和qsort()函数,atoi()计耗时,qsort()排序。

        每输入一道题的提交状态就进行一次判断,不必全部存放再判断。

参考代码:

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

typedef struct Stu {
	char name[11]; // 名字
	int score; // 得分
	int ptime; // 耗时
} Stu;

int cmp(const void *a, const void *b) {

	Stu *c = (Stu *)a;
	Stu *d = (Stu *)b;

	if (c->score != d->score) return d->score - c->score; // 若得分不同时,按得分降序

	if (c->ptime != d->ptime) return c->ptime - d->ptime; // 否则,若耗时不同,按罚时升序

	return strcmp(c->name, d->name); // 否则,按名字字典序
}

void Judge(Stu *stu, char *tmp, int m) {

	if (tmp[0] == '-' || tmp[0] == '0') return; // WA或提交0,不计
        
        // AC
	stu->score++; // 得分++
	stu->ptime += atoi(tmp); // 先计耗时

	if (strlen(tmp) >= 4) { // 由题意,AC耗时小于1000,len >=4 为x(x)的情况

		int i = 0;
		while (tmp[++i] != '('); // 找到左括号位置i
		stu->ptime += atoi(tmp + i + 1) * m; // 计罚时
	}
}

int main()
{
	Stu stus[1000];
	int i, n, m, total = 0;
	char tmp[10];

	scanf("%d%d", &n, &m);
	getchar();
	while (scanf("%s", stus[total].name) != EOF) {

		stus[total].score = 0;
		stus[total].ptime = 0;
		for (i = 0; i < n; i++) {

			scanf("%s", tmp);
			Judge(&stus[total], tmp, m);
		}
		getchar();
		total++;
	}
	qsort(stus, total, sizeof(stus[0]), cmp);

	for (i = 0; i < total; i++) {

		printf("%-10s %2d %4d\n", stus[i].name, stus[i].score, stus[i].ptime);
	}
	return 0;
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区