《排课表》题解

· · 个人记录

题意简述

无。

做法

依题意模拟即可。

对于每节课,记录以下内容:

每节课前,读入操作并处理:

处理完后,即可输出这节课的课程安排。

核心代码讲解

读入

给输入的科目名称标号。

定义一个 map 用来记录每个科目名称对应的编号。

#include<map>

int tot;//编号
map<string,int>a;

for(int i=1;i<=5;i++)
    for(int j=1;j<=7;j++)
    {
        string s;
        cin>>s;
        if(!a[s]) a[s]=++tot;//之前没编过号,给它编号
        mp[i][j].wish[a[s]]={s,n,0};//初始意愿度记录
    }

课程存储

每节课的信息单独存储。

struct sub{string s;int w,t;};
struct table
{
    int cnt;
    sub wish[50];
    int f;
    string order;
}mp[10][10];

其中,wish 数组记录每个科目老师对该节课的意愿度,f 记录校长是否指定,order 记录校长指定的科目名称。
sub 结构体中的 s 为科目名称,w 为意愿度,t 为最后修改的时间(第一节课前为 1,以此类推)。

输出

void print(int x,int y)//输出第x天第y节课的课程安排
{
    if(mp[x][y].f)//如果校长有指定
    {
        cout<<mp[x][y].order<<" ";
        return;
    }
    int mx=0,t=-1;//mx为当前最大意愿度,t为最大意愿度对应的时间
    string s;//最大意愿度的科目名称
    for(int i=1;i<=tot;i++)
    {
        sub now=mp[x][y].wish[i];
        if(now.w>mx) mx=now.w,t=now.t,s=now.s;
        else if(now.w==mx&&now.t>t) t=now.t,s=now.s;
    }
    if(mx) cout<<s<<" ";//最大意愿度不为0
    else cout<<"Zixi ";//自习课
    return;
}

完整代码

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int n,tot;
map<string,int>a;
struct sub{string s;int w,t;};
struct table
{
    int cnt;
    sub wish[50];
    int f;
    string order;
}mp[10][10];
void print(int x,int y)
{
    if(mp[x][y].f)
    {
        cout<<mp[x][y].order<<" ";
        return;
    }
    int mx=0,t=-1;string s;
    for(int i=1;i<=tot;i++)
    {
        sub now=mp[x][y].wish[i];
        if(now.w>mx) mx=now.w,t=now.t,s=now.s;
        else if(now.w==mx&&now.t>t) t=now.t,s=now.s;
    }
    if(mx) cout<<s<<" ";
    else cout<<"Zixi ";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=5;i++)
        for(int j=1;j<=7;j++)
        {
            string s;
            cin>>s;
            if(!a[s]) a[s]=++tot;
            mp[i][j].wish[a[s]]={s,n,0};
        }
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=7;j++)
        {
            int op,x,y,w;
            string s;
            cin>>op>>s;
            if(op==2)
            {
                cin>>x>>y>>w;
                mp[x][y].wish[a[s]]={s,w,(i-1)*7+j};
            }
            else if(op==3)
            {
                cin>>x>>y;
                mp[x][y].f=1,mp[x][y].order=s;
            }
            print(i,j);//处理完操作立马输出当前课程安排
        }
        cout<<"\n";
    }
    return 0;
}