UVa

Luogu

分析

其实写这篇只是为了纪念一下写的第一道大模拟(

暴力模拟就好了,查询都直接暴力扫一遍,因为输入只有 10KB 所以根本不要担心 TLE。

我是用 std::vector 维护的所有学生,这样子在删除时可以和最后的元素交换一下然后 pop_back

代码除了奇妙的换行之外也没啥细节。

代码

打表一时爽,一直打表一直爽

// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
using namespace std;
typedef long long ll;

int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const double eps=1e-8;

int tim=0;

struct Student {
    string sid; int cid; string name;
    int chinese,math,english,programming,sumscore;
    int addtim;
    Student(string a,int b,string c,int d,int e,int f,int g) {
        sid=a,cid=b,name=c;
        chinese=d,math=e,english=f,programming=g;
        sumscore=d+e+f+g;
        addtim=++tim;
    }
};
bool cmpt(Student a,Student b) { return a.addtim<b.addtim; }
vector<Student> vec;

void Print(Student x) {
    int rnk=1;
    for (auto i:vec) if (i.sumscore>x.sumscore) ++rnk;
    cout<<rnk<<" "<<x.sid<<" "<<x.cid<<" "<<x.name<<" "
        <<x.chinese<<" "<<x.math<<" "<<x.english<<" "<<x.programming<<" "
        <<x.sumscore<<" "<<fixed<<setprecision(2)<<x.sumscore/4.0+eps<<endl;
}

void Add() {
    while (1) {
        cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
        string sid,name; int cid,chinese,math,english,programming;
        cin>>sid; if (sid=="0") break;
        cin>>cid>>name>>chinese>>math>>english>>programming;
        int flag=0;
        for (auto i:vec) if (i.sid==sid) { flag=1; break; }
        if (flag) { cout<<"Duplicated SID."<<endl; continue; }
        vec.emplace_back(Student(sid,cid,name,chinese,math,english,programming));
    }
}

void Remove() {
    while (1) {
        cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
        string str; cin>>str; if (str=="0") break;
        int cnt=0;
        for (int i=0;i<vec.size();++i)
            if (vec[i].sid==str||vec[i].name==str)
                swap(vec[i],vec[vec.size()-1]),vec.pop_back(),++cnt,--i;
        cout<<cnt<<" student(s) removed."<<endl;
    }
}

void Query() {
    while (1) {
        cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
        string str; cin>>str; if (str=="0") break;
        vector<Student> v; v.clear();
        for (auto i:vec)
            if (i.sid==str||i.name==str) v.emplace_back(i);
        sort(v.begin(),v.end(),cmpt);
        for (auto i:v) Print(i);
    }
}

void ShowRanking() {
    cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."<<endl;
}

void ShowStatistics() {
    cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
    int cid=read();
    int cnt=0,p[5]={0,0,0,0,0};
    int schinese=0,smath=0,senglish=0,sprogramming=0;
    int pchinese=0,pmath=0,penglish=0,pprogramming=0;
    for (auto i:vec) {
        if (cid&&i.cid!=cid) continue;
        ++cnt;
        schinese+=i.chinese,pchinese+=i.chinese>=60;
        smath+=i.math,pmath+=i.math>=60;
        senglish+=i.english,penglish+=i.english>=60;
        sprogramming+=i.programming,pprogramming+=i.programming>=60;
        ++p[(i.chinese>=60)+(i.math>=60)+(i.english>=60)+(i.programming>=60)];
    }
    cout<<"Chinese"<<endl;
    cout<<"Average Score: "<<fixed<<setprecision(2)<<1.0*schinese/cnt+eps<<endl;
    cout<<"Number of passed students: "<<pchinese<<endl;
    cout<<"Number of failed students: "<<cnt-pchinese<<endl;
    cout<<endl;
    cout<<"Mathematics"<<endl;
    cout<<"Average Score: "<<fixed<<setprecision(2)<<1.0*smath/cnt+eps<<endl;
    cout<<"Number of passed students: "<<pmath<<endl;
    cout<<"Number of failed students: "<<cnt-pmath<<endl;
    cout<<endl;
    cout<<"English"<<endl;
    cout<<"Average Score: "<<fixed<<setprecision(2)<<1.0*senglish/cnt+eps<<endl;
    cout<<"Number of passed students: "<<penglish<<endl;
    cout<<"Number of failed students: "<<cnt-penglish<<endl;
    cout<<endl;
    cout<<"Programming"<<endl;
    cout<<"Average Score: "<<fixed<<setprecision(2)<<1.0*sprogramming/cnt+eps<<endl;
    cout<<"Number of passed students: "<<pprogramming<<endl;
    cout<<"Number of failed students: "<<cnt-pprogramming<<endl;
    cout<<endl;
    cout<<"Overall:"<<endl;
    cout<<"Number of students who passed all subjects: "<<p[4]<<endl;
    cout<<"Number of students who passed 3 or more subjects: "<<p[3]+p[4]<<endl;
    cout<<"Number of students who passed 2 or more subjects: "<<p[2]+p[3]+p[4]<<endl;
    cout<<"Number of students who passed 1 or more subjects: "<<p[1]+p[2]+p[3]+p[4]<<endl;
    cout<<"Number of students who failed all subjects: "<<p[0]<<endl;
    cout<<endl;
}

int main() {
    while (1) {
        cout<<"Welcome to Student Performance Management System (SPMS)."<<endl;
        cout<<""<<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;
        cout<<""<<endl;
        int op=read();
        if (op==0) break;
        if (op==1) Add();
        if (op==2) Remove();
        if (op==3) Query();
        if (op==4) ShowRanking();
        if (op==5) ShowStatistics();
    }
    return 0;
}
最后修改:2020 年 08 月 08 日 10 : 17 PM