跳到正文

个人记录

《排课表》题解

luohoujunyang约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年3月29日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 5 分钟

版本

  1. v1当前

题意简述

无。

做法

依题意模拟即可。

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

  • 每个科目老师对它的意愿度;
  • 校长是否对它有指定;
  • 若校长有指定,指定的科目名称。

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

  • 若为 1 操作,什么都不做;
  • 若为 2 操作,则对相应的课修改该科目的意愿度;
  • 若为 3 操作,则对相应的课标记。

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

核心代码讲解

读入

给输入的科目名称标号。

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

C++
#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};//初始意愿度记录
	}

课程存储

每节课的信息单独存储。

C++
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 为最后修改的时间(第一节课前为 11,以此类推)。

输出

C++
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;
}

完整代码

C++
#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;
}
原文