• 12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) (UVA)


    题目链接如下:

    Online Judge

    这道题折磨了我很久。最后是用了如何比较两个txt文件内容的细微差别-百度经验

    这里介绍的fc 1.txt 2.txt 来比较两个答案的差别,最后查出了bug。用的测试样例是UDebug上的一组大数据:12412 | uDebug

    为了比较答案,另外还学到了下面这个很有用的用法。

    1. #define debug
    2. #ifdef debug
    3. freopen("0.txt","r",stdin);
    4. freopen("1.txt","w",stdout);
    5. #endif
    6. #ifdef debug
    7. fclose(stdin);
    8. fclose(stdout);
    9. #endif

    我起先的代码问题出在:一个student被删除后,可以重新加回;但我是用mp[sid](map)来判断这个student还存不存在,被加回后,起先被删除但仍留在vector中的也变成存在了,造成了vector中有了两个重复的student。所以只能在student的结构体中加入了existed这个判断是否存在的变量。(确切来说,mp[sid]可以判断学生是否存在,若存在,则在vec[mp[sid] - 1]这个位置上;existed判断vector上这个student是否存在。)

    我的代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. // #define debug
    7. struct stu{
    8. std::string sid;
    9. int cid;
    10. std::string name;
    11. std::vector<int> score = std::vector<int> (4);
    12. int rank;
    13. int totalScore = 0;
    14. int existed = 1;
    15. };
    16. struct course{
    17. int tot = 0;
    18. int passed = 0;
    19. int failed = 0;
    20. };
    21. int cnt, task;
    22. std::vector vec, vec1;
    23. std::mapint> mp;
    24. std::string fourCourse[] = {"Chinese", "Mathematics", "English", "Programming"};
    25. void mainmenu(){
    26. printf("Welcome to Student Performance Management System (SPMS).\n\n");
    27. printf("1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
    28. }
    29. void addStudent(){
    30. stu temp;
    31. while(1){
    32. printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
    33. std::cin >> temp.sid;
    34. if(temp.sid == "0"){
    35. break;
    36. }
    37. scanf("%d", &temp.cid);
    38. std::cin >> temp.name;
    39. temp.totalScore = 0;
    40. for(int i = 0; i < 4; ++i){
    41. scanf("%d", &temp.score[i]);
    42. temp.totalScore += temp.score[i];
    43. }
    44. if(mp[temp.sid]){
    45. printf("Duplicated SID.\n");
    46. } else{
    47. mp[temp.sid] = ++cnt;
    48. vec.push_back(temp);
    49. }
    50. }
    51. }
    52. void removeStudent(){
    53. std::string str;
    54. while(1){
    55. printf("Please enter SID or name. Enter 0 to finish.\n");
    56. std::cin >> str;
    57. if(str == "0"){
    58. break;
    59. }
    60. int xx = 0;
    61. if(mp[str]){
    62. vec[mp[str] - 1].existed = 0;
    63. mp[str] = 0;
    64. xx = 1;
    65. } else{
    66. for(int i = 0; i < vec.size(); ++i){
    67. if(vec[i].existed && vec[i].name == str){
    68. ++xx;
    69. vec[i].existed = 0;
    70. mp[vec[i].sid] = 0;
    71. }
    72. }
    73. }
    74. printf("%d student(s) removed.\n", xx);
    75. }
    76. }
    77. bool cmp1(const stu &a, const stu &b){
    78. return a.totalScore > b.totalScore;
    79. }
    80. bool cmp2(const stu &a, const stu &b){
    81. return mp[a.sid] < mp[b.sid];
    82. }
    83. void queryStudent(){
    84. vec1.clear();
    85. for(int i = 0; i < vec.size(); ++i){
    86. if(vec[i].existed){
    87. vec1.push_back(vec[i]);
    88. }
    89. }
    90. sort(vec1.begin(), vec1.end(), cmp1);
    91. vec1[0].rank = 1;
    92. for(int i = 1; i < vec1.size(); ++i){
    93. if(vec1[i].totalScore == vec1[i - 1].totalScore){
    94. vec1[i].rank = vec1[i - 1].rank;
    95. } else{
    96. vec1[i].rank = i + 1;
    97. }
    98. }
    99. sort(vec1.begin(), vec1.end(), cmp2);
    100. std::string str;
    101. while(1){
    102. printf("Please enter SID or name. Enter 0 to finish.\n");
    103. std::cin >> str;
    104. if(str == "0"){
    105. break;
    106. }
    107. for(int i = 0; i < vec1.size(); ++i){
    108. if(vec1[i].sid == str || vec1[i].name == str){
    109. printf("%d %s %d %s %d %d %d %d %d %.2f\n", vec1[i].rank, vec1[i].sid.c_str(), vec1[i].cid,
    110. vec1[i].name.c_str(), vec1[i].score[0], vec1[i].score[1], vec1[i].score[2], vec1[i].score[3],
    111. vec1[i].totalScore, vec1[i].totalScore / 4.0 + 0.00001);
    112. }
    113. }
    114. }
    115. }
    116. void showStatistics(){
    117. printf("Please enter class ID, 0 for the whole statistics.\n");
    118. int cid;
    119. scanf("%d", &cid);
    120. course a[4];
    121. int tot[5] = {0, 0, 0, 0, 0};
    122. int stuNbr = 0;
    123. int p = 0;
    124. for(int i = 0; i < vec.size(); ++i){
    125. if(vec[i].existed && (vec[i].cid == cid || !cid)){
    126. stuNbr++;
    127. int failNum = 0;
    128. for(int j = 0; j < 4; ++j){
    129. a[j].tot += vec[i].score[j];
    130. if(vec[i].score[j] >= 60){
    131. a[j].passed++;
    132. } else{
    133. a[j].failed++;
    134. failNum++;
    135. }
    136. }
    137. tot[failNum]++;
    138. }
    139. }
    140. for(int i = 0; i < 4; ++i){
    141. printf("%s\n", fourCourse[i].c_str());
    142. printf("Average Score: %.2f\n", stuNbr == 0 ? 0 : a[i].tot * 1.0 / stuNbr + 0.00001);
    143. printf("Number of passed students: %d\n", a[i].passed);
    144. printf("Number of failed students: %d\n\n", a[i].failed);
    145. }
    146. p += tot[0];
    147. printf("Overall:\nNumber of students who passed all subjects: %d\n", p);
    148. for(int i = 1; i <= 3; ++i){
    149. p += tot[i];
    150. printf("Number of students who passed %d or more subjects: %d\n", 4 - i, p);
    151. }
    152. printf("Number of students who failed all subjects: %d\n\n", tot[4]);
    153. }
    154. int main(){
    155. #ifdef debug
    156. freopen("0.txt","r",stdin);
    157. freopen("1.txt","w",stdout);
    158. #endif
    159. cnt = 0;
    160. while(1){
    161. mainmenu();
    162. scanf("%d", &task);
    163. if(!task){
    164. break;
    165. }
    166. if(task == 1){
    167. addStudent();
    168. } else if(task == 2){
    169. removeStudent();
    170. } else if(task == 3){
    171. queryStudent();
    172. } else if(task == 4){
    173. printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
    174. } else{
    175. showStatistics();
    176. }
    177. }
    178. #ifdef debug
    179. fclose(stdin);
    180. fclose(stdout);
    181. #endif
    182. return 0;
    183. }

  • 相关阅读:
    Redis深度历险
    基于QT的考试管理系统设计与实现
    【机器学习】21天挑战赛学习笔记(六)
    Android-NDK开发——基本概念
    45 二叉树的右视图
    设置Oracle数据库默认为spfle启动,并且设置数据库SGA大小和PGA大小
    【无标题】
    Vue3 + Vite2 项目实战复盘总结(干货!)
    需要打开多少监控器
    CCNA笔记
  • 原文地址:https://blog.csdn.net/linh2006/article/details/133129121