• 【例题4-6 uva12412】A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)


    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    训练编程的题。 原题中没有除0的数据,所以别担心你的代码是因为除0错了。 多半跟我一样。 也是因为没有+eps 就是比如你要算tot/4的值。 那么要输出tot/4+1e-6 不然会错。。。 浮点误差...

    剩下的。其实很简答的。。
    注意有并列第x的情况就好。

    【代码】

    #include <bits/stdc++.h>
    using namespace std;
    
    const string subject[4]={"Chinese","Mathematics","English","Programming"};
    
    struct abc{
        string sid,cid,name;
        int goal[4];int tot;
    };
    
    int ope;
    vector<abc> v;
    vector<abc> temp;
    map<string,int> ranking;
    
    void output_message(){
        cout<<"Welcome to Student Performance Management System (SPMS)."<<endl<<endl;
        cout<<"1 - Add"<<endl;
        cout<<"2 - Remove"<<endl;
        cout<<"3 - Query"<<endl;
        cout<<"4 - Show ranking"<<endl;
        cout<<"5 - Show Statistics"<<endl;
        cout<<"0 - Exit"<<endl<<endl;
    }
    
    void tip(int idx){
        if (idx==1) cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
        if (idx==2) cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
        if (idx==3) cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
        if (idx==4) cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."<<endl;
        if (idx==5) cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
    }
    
    bool found_student_by_sid(string sid){
        for (abc temp:v) if (temp.sid==sid) return true;
        return false;
    }
    
    int _remove(string par,int p){
        int cnt = 0;
        vector<abc> newv;newv.clear();
        for (int i = 0;i < (int) v.size();i++){
            if ( (p==0 && v[i].sid == par) || (p==1 && v[i].name == par)  ){
                cnt++;
            }else newv.push_back(v[i]);
        }
        v.resize((int)newv.size());
        v = newv;
        return cnt;
    }
    
    bool cmp(abc a,abc b){
        return a.tot>b.tot;
    }
    
    int main(){
        //freopen("/home/ccy/rush.txt","r",stdin);
        //freopen("/home/ccy/rush_out.txt","w",stdout);
        ios::sync_with_stdio(0),cin.tie(0);
        while (1){
            output_message();
            cin >> ope;
            if (ope==1){
                while(1){
                    tip(1);
                    abc temp;
                    cin >> temp.sid;
                    if (temp.sid=="0") break;
                    temp.tot = 0;
                    cin >> temp.cid >> temp.name;
                    for (int i = 0;i < 4;i++){
                        cin >> temp.goal[i];
                        temp.tot+=temp.goal[i];
                    }
                    if (found_student_by_sid(temp.sid))
                        cout<<"Duplicated SID."<<endl;
                    else
                        v.push_back(temp);
                }
            }
            if (ope==2){
                while (1){
                    tip(2);
                    string s;
                    cin >> s;
                    if (s=="0") break;
                    int p = 0;
                    if (s[0]>='A' && s[0]<='Z') p = 1;
                    cout<<_remove(s,p)<<" student(s) removed."<<endl;
                }
            }
            if (ope==3){
                while (1){
                    tip(3);
                    string s;
                    cin >> s;
                    if (s=="0") break;
                    temp.resize((int)v.size());
                    for (int i = 0;i < (int)v.size();i++) temp[i] = v[i];
                    sort(temp.begin(),temp.end(),cmp);
                    for (int i = 0;i < (int)v.size();i++){
                        int cur = i+1,j = i;
                        ranking[temp[i].sid] = cur;
                        while (j+1<(int)v.size() && temp[j+1].tot==temp[i].tot) {
                            j++;
                            ranking[temp[j].sid] = cur;
                        }
                        i = j;
                    }
                    int p = 0;
                    if (s[0]>='A' && s[0]<='z') p = 1;
                    for (int i = 0;i < (int)v.size();i++){
                        if ( (p==0 && v[i].sid == s) || (p==1 && v[i].name==s) ){
                            cout<<ranking[v[i].sid]<<" ";
                            cout<<v[i].sid<<" "<<v[i].cid<<" "<<v[i].name<<" ";
                            for (int j = 0;j < 4;j++) cout<<v[i].goal[j]<<" ";
                            cout<<v[i].tot<<" ";
                            cout<<fixed<<setprecision(2)<<1.0*v[i].tot/4.0+(1e-5)<<endl;
                        }
                    }
                }
            }
            if (ope==4) tip(4);
            if (ope==5){
                tip(5);
                string s;
                cin >> s;
                for (int i = 0;i < 4;i++){
                    int cntb60 = 0,cntl60 = 0,tot =0,cnt = 0;
                    for (int j = 0;j < (int)v.size();j++){
                        if (v[j].cid==s || s=="0"){
                            if (v[j].goal[i]>=60){
                                cntb60++;
                            }else cntl60++;
                            tot+=v[j].goal[i];
                            cnt++;
                        }
                    }
                    cout<<subject[i]<<endl;
                    cout<<"Average Score: ";
                    cout<<fixed<<setprecision(2)<<1.0*tot/(1.0*cnt)+(1e-5)<<endl;
    
                    cout<<"Number of passed students: "<<cntb60<<endl;
                    cout<<"Number of failed students: "<<cntl60<<endl;
                    cout<<endl;
                }
               cout<<"Overall:"<<endl;
               int cnt[5]={0};
               for (int i = 0;i < (int)v.size();i++)
                   if (v[i].cid==s || s=="0"){
                      int temp = 0;
                      for (int j = 0;j < 4;j++){
                        if (v[i].goal[j]>=60) temp++;
                      }
                      cnt[temp]++;
                   }
               cout<<"Number of students who passed all subjects: "<<cnt[4]<<endl;
               for (int i = 3;i >= 1;i--) cnt[i]+=cnt[i+1];
               for (int i = 3;i >= 1;i--)
                    cout<<"Number of students who passed "<<i<<" or more subjects: "<<cnt[i]<<endl;
               cout<<"Number of students who failed all subjects: "<<cnt[0]<<endl;
               cout<<endl;
            }
    
            if (ope==0){
                break;
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    二叉搜索树与双向链表
    TCP 三次握手与四次挥手
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
    栈的压入、弹出序列
    jenkins 持续集成和交付——一个构件小栗子前置(三)
    jenkins 持续集成和交付——gogs安装(外篇)
    jenkins 持续集成和交付——安装与账户安全还有凭证(二)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/9845894.html
Copyright © 2020-2023  润新知