MEGChai MEGChai
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
首页 › 数据结构与算法 › 在线评测 › UVaOJ 12412 - A Typical Homework
AOAPC II

UVaOJ 12412 - A Typical Homework

Chai
2021-11-24 0:00:00在线评测阅读 320

问题描述

p12412

原题链接:UVaOJ 12412 - A Typical Homework

相关说明:本题为《算法竞赛入门经典(第2版)》例题 4-6

解法一:模拟

对于初学者来说是比较繁琐的一道大模拟题,仅写了 C++ 实现,注意事项:

  • 我的实现中用数组来存储 4 门课程的相关统计信息,方便写成循环形式;
  • 我的实现中没有每次在更新数据后做“排序”操作,而是每次查询即时计算 rank;
  • 原题输出样例中的 ` 符号有误,应当是 ' 符号(代码中用注释进行了说明)。

感觉这道题确实适合作为 C++ 语言课程设计大作业,但是作为算法竞赛题有些迷。

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <bits/stdc++.h>
 
using namespace std;
 
struct Student {
  string SID;  // Student ID
  string CID;  // Class ID
  string name;
  int score[4];  // "Chinese", "Mathematics", "English", "Programming"
  int total_score;
  float average_score;
};
 
vector<Student> database;
 
void show_menu() {
  cout << "Welcome to Student Performance Management System (SPMS)." << endl
       << endl
       << "1 - Add" << endl
       << "2 - Remove" << endl
       << "3 - Query" << endl
       << "4 - Show ranking" << endl
       << "5 - Show Statistics" << endl
       << "0 - Exit" << endl
       << endl;
}
 
void do_add() {
  Student tmp;
  while (cout << "Please enter the SID, CID, name and four scores. "
              << "Enter 0 to finish." << endl &&
         cin >> tmp.SID && tmp.SID != "0") {
    cin >> tmp.CID >> tmp.name >> tmp.score[0] >> tmp.score[1] >>
        tmp.score[2] >> tmp.score[3];
    tmp.total_score = tmp.score[0] + tmp.score[1] + tmp.score[2] + tmp.score[3];
    tmp.average_score = 1.0 * tmp.total_score / 4 + 1e-5;
    bool duplicated = false;
    for (auto item : database) {
      if (tmp.SID == item.SID) {
        cout << "Duplicated SID." << endl;
        duplicated = true;
      }
    }
    if (!duplicated) database.push_back(tmp);
  }
}
 
void do_remove() {
  string target;
  while (cout << "Please enter SID or name. Enter 0 to finish." << endl &&
         cin >> target && target != "0") {
    int cnt = 0;
    for (auto it = database.begin(); it != database.end();) {
      if (it->SID == target || it->name == target) {
        database.erase(it);
        cnt += 1;
      } else {
        ++it;
      }
    }
    cout << cnt << " student(s) removed." << endl;
  }
}
 
int find_rank(int total_score) {
  int rank = 1;
  for (auto it = database.begin(); it != database.end(); ++it)
    if (total_score < it->total_score) rank += 1;
  return rank;
}
 
void do_query() {
  string target;
  while (cout << "Please enter SID or name. Enter 0 to finish." << endl &&
         cin >> target && target != "0")
    for (auto it = database.begin(); it != database.end(); ++it)
      if (it->SID == target || it->name == target)
        cout << find_rank(it->total_score) << " " << it->SID << " " << it->CID
             << " " << it->name << " " << it->score[0] << " " << it->score[1]
             << " " << it->score[2] << " " << it->score[3] << " "
             << it->total_score << " " << it->average_score << endl;
}
 
void do_show_ranking() {
  cout << "Showing the ranklist hurts students' self-esteem. "
       << "Don't do that." << endl;  // Output example is wrong (fix ’ to ')
}
 
void do_show_statistics() {
  string target,
      course[4] = {"Chinese", "Mathematics", "English", "Programming"};
  int passed[4] = {0}, failed[4] = {0}, pass_num[5] = {0}, student_cnt = 0;
  float total_score[4] = {0}, average_score[4] = {0};
 
  cout << "Please enter class ID, 0 for the whole statistics." << endl;
  cin >> target;
  for (auto it = database.begin(); it != database.end(); ++it) {
    if (target == "0" || it->CID == target) {
      student_cnt += 1;
      int pass_cnt = 0;
 
      for (int i = 0; i < 4; ++i) {
        total_score[i] += it->score[i];
        if (it->score[i] >= 60) {
          passed[i] += 1;
          pass_cnt += 1;
        } else {
          failed[i] += 1;
        }
      }
 
      pass_num[pass_cnt] += 1;
    }
  }
 
  for (int i = 0; i < 4; ++i) {
    cout << course[i] << endl
         << "Average Score: " << total_score[i] / student_cnt + 1e-5 << endl
         << "Number of passed students: " << passed[i] << endl
         << "Number of failed students: " << failed[i] << endl
         << endl;
  }
 
  cout << "Overall:" << endl
       << "Number of students who passed all subjects: " << pass_num[4] << endl
       << "Number of students who passed 3 or more subjects: "
       << pass_num[4] + pass_num[3] << endl
       << "Number of students who passed 2 or more subjects: "
       << pass_num[4] + pass_num[3] + pass_num[2] << endl
       << "Number of students who passed 1 or more subjects: "
       << pass_num[4] + pass_num[3] + pass_num[2] + pass_num[1] << endl
       << "Number of students who failed all subjects: " << pass_num[0] << endl
       << endl;
}
 
int main() {
  int choice;
  while (true) {
    show_menu();
    cin >> choice;
    cout << fixed << setprecision(2);
    if (!choice) break;
    if (choice == 1) do_add();
    if (choice == 2) do_remove();
    if (choice == 3) do_query();
    if (choice == 4) do_show_ranking();
    if (choice == 5) do_show_statistics();
  }
  return 0;
}
Python
1
#TODO
AOAPC II UVaOJ 字符串
赞赏 赞(0)
订阅
提醒
guest
guest
0 评论
内嵌评论
查看所有评论
  • 0
  • 0
Copyright © 2020-2023 MEGChai.
  • 文章
    • 随笔
    • 笔记
    • 教程
  • 关于
# 生活 # # 心理 # # 编程 # # 音乐 # # 写作 #
Chai
95
文章
4
评论
58
喜欢
wpDiscuz