原题链接:考试排名
解题思路:
主要用到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 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复