Problem Description 今天的上機考試雖然有實時的Ranklist,但上面的排名只是根據完成的題數排序,沒有考慮
4 5 25 10 10 12 13 15 CS004 3 5 1 3 CS003 5 2 4 1 3 5 CS002 2 1 2 CS001 3 2 3 5 1 2 40 10 30 CS001 1 2 2 3 20 10 10 10 CS000000000000000001 0 CS000000000000000002 2 1 2 0
3 CS003 60 CS001 37 CS004 37 0 1 CS000000000000000002 20
思路:都會的思路;
不是思路難,是有點繁的題目,速度是關鍵。
這樣的題目是用來訓練細心和編碼速度的。
#include#include #include using namespace std; struct Stu { char *num; int grade; bool operator<(const Stu b) const { if (grade == b.grade) return strcmp(num, b.num) == -1; return grade > b.grade; } }; Stu stus[1001];//N int grades[12];//M char stuNum[22]; int main() { int N, M, G; while (scanf("%d %d %d", &N, &M, &G) == 3) { for (int i = 1; i <= M; i++) { scanf("%d", &grades[i]); } int count = 0; for (int i = 0; i < N; i++) { int totalGra = 0, totalNum = 0, m, id; scanf("%s %d", stuNum, &m); for (int j = 0; j < m; j++) { scanf(" %d", &id);//沒看清楚題意就動手,以至浪費時間 totalGra += grades[id]; } if (totalGra >= G) { stus[count].grade = totalGra; stus[count].num = (char *) malloc(sizeof(stuNum)+1); strcpy(stus[count].num, stuNum); count++; } } sort(stus, stus+count); printf("%d\n", count); for (int j = 0; j < count; j++) { printf("%s ", stus[j].num); printf("%d\n", stus[j].grade); free(stus[j].num); } } return 0; }