UVa 12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) 【模拟】

来源:互联网 发布:管家婆软件价格 编辑:程序博客网 时间:2024/05/16 12:04

题目链接:UVa 12412


A Typical Homework(a.k.a Shi Xiong Bang Bang Mang)

Hi, I am an undergraduate student in institute of foreign languages. As you know, C programming is a required course in our university, even if his/her major is far from computer science. I don't like this course at all, as I am not good at computer and I don't wanna even have a try of any programming!! But I have to do the homework in order to pass :( Sh... Could you help me with it? Please keep secret!! I know that you won't say NO to a poor little girl, boy. :)

Task

Write a Student Performance Management System (SPMS).

Concepts

In the SPMS, there will be at most 100 students, each has an SID, a CID, a name and four scores (Chinese, Mathematics, English and Programming).
  • SID (student ID) is a 10-digit number
  • CID (class ID) is a positive integer not greater than 20.
  • Name is a string of no more than 10 letters and digits, beginning with a capital letter. Note that a name cannot contain space characters inside.
  • Each score is a non-negative integer not greater than 100.

Main Menu

When you enter the SPMS, the main menu should be shown like this:
Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - Exit

Adding a Student

If you choose 1 from the main menu, the following message should be printed on the screen:
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Then your program should wait for user input. The input lines are always valid (no invalid SID, CID, or name, exactly four scores etc), but the SID may already exist. In that case, simply ignore this line and print the following:
Duplicated SID.
On the other hand, multiple students can have the same name. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.

Removing a Student

If you choose 2 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input, and remove all the students matching the SID or name in the database, and print the following message (it's possible xx=0):
xx student(s) removed.
You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.

Querying Students

If you choose 3 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input. If no student matches the SID or name, simply do nothing, otherwise print out all the matching students, in the same order they're added to the database. The format is similar to the input format for "adding a student", but 3 more columns are added: rank (1st column), total score and average score (last two columns). The student with highest total score (considering all classes) received rank-1, and if there are two rank-2 students, the next one would be rank-4. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.

Showing the Ranklist

If you choose 4 from the main menu, the following message should be printed on the screen:
Showing the ranklist hurts students' self-esteem. Don't do that.
Then the main menu is printed again.

Showing Statistics

If you choose 5 from the main menu, show the statistics, in the following format:
Please enter class ID, 0 for the whole statistics.
When a class ID is entered, print the following statistics. Note that "passed" means to have a score of at least 60.
ChineseAverage Score: xx.xxNumber of passed students: xxNumber of failed students: xxMathematicsAverage Score: xx.xxNumber of passed students: xxNumber of failed students: xxEnglishAverage Score: xx.xxNumber of passed students: xxNumber of failed students: xxProgrammingAverage Score: xx.xxNumber of passed students: xxNumber of failed students: xxOverall:Number of students who passed all subjects: xxNumber of students who passed 3 or more subjects: xxNumber of students who passed 2 or more subjects: xxNumber of students who passed 1 or more subjects: xxNumber of students who failed all subjects: xx
Then, the main menu is printed again.

Exiting SPMS

If you choose 0 from the main menu, the program should terminate. Note that course scores and total score should be formatted as integers, but average scores should be formatted as a real number with exactly two digits after the decimal point.

Input

There will be a single test case, ending with a zero entered in the main menu screen. The entire input will be valid. The size of input does not exceed 10KB.

Output

Print out everything as stated in the problem description. You should be able to play around this little program in your machine, with a keyboard and a screen. However, both the input and output may look silly when they're not mixed, as in the keyboard-screen case.

Sample Input

10011223344 1 John 79 98 91 1000022334455 1 Tom 59 72 60 810011223344 2 Alice 100 100 100 1002423475629 2 John 60 80 30 99030022334455John0512001122334405040

Output for the Sample Input

Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitPlease enter the SID, CID, name and four scores. Enter 0 to finish.Please enter the SID, CID, name and four scores. Enter 0 to finish.Please enter the SID, CID, name and four scores. Enter 0 to finish.Duplicated SID.Please enter the SID, CID, name and four scores. Enter 0 to finish.Please enter the SID, CID, name and four scores. Enter 0 to finish.Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitPlease enter SID or name. Enter 0 to finish.2 0022334455 1 Tom 59 72 60 81 272 68.00Please enter SID or name. Enter 0 to finish.1 0011223344 1 John 79 98 91 100 368 92.003 2423475629 2 John 60 80 30 99 269 67.25Please enter SID or name. Enter 0 to finish.Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitPlease enter class ID, 0 for the whole statistics.ChineseAverage Score: 69.00Number of passed students: 1Number of failed students: 1MathematicsAverage Score: 85.00Number of passed students: 2Number of failed students: 0EnglishAverage Score: 75.50Number of passed students: 2Number of failed students: 0ProgrammingAverage Score: 90.50Number of passed students: 2Number of failed students: 0Overall:Number of students who passed all subjects: 1Number of students who passed 3 or more subjects: 2Number of students who passed 2 or more subjects: 2Number of students who passed 1 or more subjects: 2Number of students who failed all subjects: 0Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitPlease enter SID or name. Enter 0 to finish.1 student(s) removed.Please enter SID or name. Enter 0 to finish.Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitPlease enter class ID, 0 for the whole statistics.ChineseAverage Score: 59.50Number of passed students: 1Number of failed students: 1MathematicsAverage Score: 76.00Number of passed students: 2Number of failed students: 0EnglishAverage Score: 45.00Number of passed students: 1Number of failed students: 1ProgrammingAverage Score: 90.00Number of passed students: 2Number of failed students: 0Overall:Number of students who passed all subjects: 0Number of students who passed 3 or more subjects: 2Number of students who passed 2 or more subjects: 2Number of students who passed 1 or more subjects: 2Number of students who failed all subjects: 0Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - ExitShowing the ranklist hurts students' self-esteem. Don't do that.Welcome to Student Performance Management System (SPMS).1 - Add2 - Remove3 - Query4 - Show ranking5 - Show Statistics0 - Exit

(本来没打算写,但是从网上搜题解时发现很多代码都标明蜜汁WA,所以写下这篇博客指出此题的坑点)


题目大意:

编写一个成绩管理系统,对应的每次输入都作出相应的回复。


解题思路:

直接模拟即可,有很多细节要注意。


注意细节:

1.计算平均分的时候要注意一下精度问题。为防止精度丢失,我在每个计算出的精度后面都加上了eps(1e-5)。
2.计算rank时,不要忘记考虑并列的情况。(如果有两个学生成绩并列第一,那么除他们外最高分就是第三名)。
3.当从主菜单输入4时,只返回一句话,那句话中的 ' 应该是英文的,直接从描述中复制过来是中文的。
4.看到有些博客中写到要防止除0的情况发生。我的程序中除0就是在求单科ave时可能出现的,但是经过测试,数据中并没有这种情况,所以可以不必考虑。
【注】以上就是我出现的错误,其他的细节都是很好发现的了。如果没出现这些错误但是你还过不了的话,不要着急,再仔细想想哪些细节出了问题,或者是私信/留言给我,作为一个在这上面WA到怀疑XX之后还坚持不懈最终A了这道题的快乐的咸鱼,我会乐意帮助你的。

Mycode:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAX = 10005;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-5;int op, tot;struct node{    int chi, mat, eng, pro, sum, rk;  //四课分数    string SID, CID, name;    double ave;    bool flag;    int pas;} a[MAX];bool cmp(int A, int B){    return A > B;}void hello(){    puts("Welcome to Student Performance Management System (SPMS).");    puts("");    puts("1 - Add");    puts("2 - Remove");    puts("3 - Query");    puts("4 - Show ranking");    puts("5 - Show Statistics");    puts("0 - Exit");    puts("");}void work1(){    string si, ci, na;    int c, m, e, p;    while(1)    {        puts("Please enter the SID, CID, name and four scores. Enter 0 to finish.");        cin >> si;        if(si == "0") return ;        cin >> ci >> na >> c >> m >> e >> p;        bool f = true;        for(int i = 0; i < tot; ++i)        {            if(a[i].flag && a[i].SID == si)            {                puts("Duplicated SID.");                f = false;                break;            }        }        if(f)        {            a[tot].SID = si;            a[tot].CID = ci;            a[tot].name = na;            a[tot].chi = c;            a[tot].eng = e;            a[tot].mat = m;            a[tot].pro = p;            a[tot].sum = c + e + m + p;            a[tot].ave = (c + e + m + p) / 4.0 + eps;            a[tot].flag = true;            ++tot;        }    }}void work2(){    string tt;    while(1)    {        puts("Please enter SID or name. Enter 0 to finish.");        cin >> tt;        if(tt == "0") return ;        int cnt = 0;        for(int i = 0; i < tot; ++i)        {            if(a[i].flag)            {                if(a[i].name == tt || a[i].SID == tt)                {                    a[i].flag = false;                    cnt++;                }            }        }        printf("%d student(s) removed.\n", cnt);    }}void work3(){    //每次查询都计算一下rank    int tem[MAX], rrk[MAX], srk = 1, now = 0;    for(int i = 0; i < tot; ++i)    {        if(a[i].flag)            tem[now++] = a[i].sum;    }    sort(tem, tem + now, cmp);    rrk[0] = 1;    srk++;    for(int i = 1; i < now; ++i)    {        if(tem[i] != tem[i-1])            rrk[i] = srk;        else            rrk[i] = rrk[i-1];        ++srk;    }    for(int i = 0; i < tot; ++i)    {        if(a[i].flag)        {            for(int t = 0; t < now; ++t)            {                if(a[i].sum == tem[t])                {                    a[i].rk = rrk[t];                    break;                }            }        }    }    string tt;    while(1)    {        puts("Please enter SID or name. Enter 0 to finish.");        cin >> tt;        if(tt == "0") return ;        for(int i = 0; i < tot; ++i)        {            if(a[i].flag && (a[i].SID == tt || a[i].name == tt))            {                cout << a[i].rk << " " << a[i].SID << " " << a[i].CID << " " << a[i].name;                printf(" %d %d %d %d %d %.2f\n", a[i].chi, a[i].mat, a[i].eng, a[i].pro, a[i].sum, a[i].ave);            }        }    }}void work4(){    puts("Showing the ranklist hurts students' self-esteem. Don't do that.");    return ;}void work5(){    puts("Please enter class ID, 0 for the whole statistics.");    string tt;    cin >> tt;    int pas, fai, yy[5], qq;    double suma, cnta, avea;    puts("Chinese");    suma = cnta = pas = fai = 0;    for(int i = 0; i < tot; ++i)    {        if(a[i].flag && (a[i].CID == tt || tt == "0"))        {            ++cnta;            suma += a[i].chi;            if(a[i].chi >= 60)                ++pas;            else                ++fai;        }    }    if(cnta == 0)        avea = 0;    else        avea = suma / cnta + eps;    printf("Average Score: %.2f\n", avea);    printf("Number of passed students: %d\n", pas);    printf("Number of failed students: %d\n", fai);    puts("");    puts("Mathematics");    suma = cnta = pas = fai = 0;    for(int i = 0; i < tot; ++i)    {        if(a[i].flag && (a[i].CID == tt || tt == "0"))        {            ++cnta;            suma += a[i].mat;            if(a[i].mat >= 60)                ++pas;            else                ++fai;        }    }    if(cnta == 0)        avea = 0;    else        avea = suma / cnta + eps;    printf("Average Score: %.2f\n", avea);    printf("Number of passed students: %d\n", pas);    printf("Number of failed students: %d\n", fai);    puts("");    puts("English");    suma = cnta = pas = fai = 0;    for(int i = 0; i < tot; ++i)    {        if(a[i].flag && (a[i].CID == tt || tt == "0"))        {            ++cnta;            suma += a[i].eng;            if(a[i].eng >= 60)                ++pas;            else                ++fai;        }    }    if(cnta == 0)        avea = 0;    else        avea = suma / cnta + eps;    printf("Average Score: %.2f\n", avea);    printf("Number of passed students: %d\n", pas);    printf("Number of failed students: %d\n", fai);    puts("");    puts("Programming");    suma = cnta = pas = fai = 0;    for(int i = 0; i < tot; ++i)    {        if(a[i].flag && (a[i].CID == tt || tt == "0"))        {            ++cnta;            suma += a[i].pro;            if(a[i].pro >= 60)                ++pas;            else                ++fai;        }    }    if(cnta == 0)        avea = 0;    else        avea = suma / cnta + eps;    printf("Average Score: %.2f\n", avea);    printf("Number of passed students: %d\n", pas);    printf("Number of failed students: %d\n", fai);    puts("");    memset(yy, 0, sizeof(yy));    puts("Overall:");    for(int i = 0; i < tot; ++i)    {        if(a[i].flag && (a[i].CID == tt || tt == "0"))        {            qq = (a[i].chi >= 60) + (a[i].eng >= 60) + (a[i].pro >= 60) + (a[i].mat >= 60);            yy[qq]++;        }    }    printf("Number of students who passed all subjects: %d\n", yy[4]);    printf("Number of students who passed 3 or more subjects: %d\n", yy[4] + yy[3]);    printf("Number of students who passed 2 or more subjects: %d\n", yy[4] + yy[3] + yy[2]);    printf("Number of students who passed 1 or more subjects: %d\n", yy[4] + yy[3] + yy[2] + yy[1]);    printf("Number of students who failed all subjects: %d\n", yy[0]);    puts("");}int main(){//    freopen("in.txt","r",stdin);//    freopen("out1.txt","w",stdout);    hello();    while(cin >> op && op)    {        if(op == 1)            work1();        else if(op == 2)            work2();        else if(op == 3)            work3();        else if(op == 4)            work4();        else if(op == 5)            work5();        hello();    }//    fclose(stdin);//    fclose(stdout);    return 0;}



阅读全文
3 0