解题思路:
主要用到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 人评分
点我有惊喜!你懂得!浏览:2212 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:552 |
钟神赛车 (C语言代码)浏览:870 |
汽水瓶 (C语言代码)浏览:597 |
C二级辅导-计负均正 (C语言代码)浏览:647 |
矩形面积交 (Java代码)浏览:1213 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:508 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)for循环浏览:1110 |
哥德巴赫曾猜测 (C语言代码)浏览:994 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:896 |