试题编号: 201609-2
试题名称: 火车购票
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配。
假设一节车厢有20排、每一排5个座位。为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10号,依次类推,第20排是96到100号。
购票时,一个人可能购一张或多张票,最多不超过5张。如果这几张票可以安排在同一排编号相邻的座位,则应该安排在编号最小的相邻座位。否则应该安排在编号最小的几个空座位中(不考虑是否相邻)。
假设初始时车票全部未被购买,现在给了一些购票指令,请你处理这些指令。
输入格式
输入的第一行包含一个整数n,表示购票指令的数量。
第二行包含n个整数,每个整数p在1到5之间,表示要购入的票数,相邻的两个数之间使用一个空格分隔。
输出格式
输出n行,每行对应一条指令的处理结果。
对于购票指令p,输出p张车票的编号,按从小到大排序。
样例输入
4
2 5 4 2
样例输出
1 2
6 7 8 9 10
11 12 13 14
3 4
样例说明
1) 购2张票,得到座位1、2。
2) 购5张票,得到座位6至10。
3) 购4张票,得到座位11至14。
4) 购2张票,得到座位3、4。
评测用例规模与约定
对于所有评测用例,1 ≤ n ≤ 100,所有购票数量之和不超过100。
明明很简单一道题,但是写的太菜了。。。
代码如下(100分):
#include <iostream>
using namespace std;
bool seat[21][6];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
int n,x;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
switch (x)
{
case 1:
{
bool flag=false;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=5;j++)
if(!seat[i][j])
{
seat[i][j]=true;
cout<<(i-1)*5+j<<endl;
flag=true;
break;
}
if(flag)
break;
}
break;
}
case 2:
{
bool flag=false;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=4;j++)
if(!seat[i][j] && !seat[i][j+1])
{
seat[i][j]=seat[i][j+1]=true;
cout<<(i-1)*5+j<<" "<<(i-1)*5+j+1<<endl;
flag=true;
break;
}
if(flag)
break;
}
if(!flag)
{
int cnt=0;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=5;j++)
{
if(!seat[i][j])
{
seat[i][j]=true;
cout<<(i-1)*5+j;
cnt++;
if(cnt==2)
{
cout<<endl;
break;
}
else
cout<<" ";
}
}
if(cnt==2)
break;
}
}
break;
}
case 3:
{
bool flag=false;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=3;j++)
if(!seat[i][j] && !seat[i][j+1] && !seat[i][j+2])
{
seat[i][j]=seat[i][j+1]=seat[i][j+2]=true;
cout<<(i-1)*5+j<<" "<<(i-1)*5+j+1<<" "<<(i-1)*5+j+2<<endl;
flag=true;
break;
}
if(flag)
break;
}
if(!flag)
{
int cnt=0;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=5;j++)
{
if(!seat[i][j])
{
seat[i][j]=true;
cout<<(i-1)*5+j;
cnt++;
if(cnt==3)
{
cout<<endl;
break;
}
else
cout<<" ";
}
}
if(cnt==3)
break;
}
}
break;
}
case 4:
{
bool flag=false;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=2;j++)
if(!seat[i][j] && !seat[i][j+1] && !seat[i][j+2] && !seat[i][j+3])
{
seat[i][j]=seat[i][j+1]=seat[i][j+2]=seat[i][j+3]=true;
cout<<(i-1)*5+j<<" "<<(i-1)*5+j+1<<" "<<(i-1)*5+j+2<<" "<<(i-1)*5+j+3<<endl;
flag=true;
break;
}
if(flag)
break;
}
if(!flag)
{
int cnt=0;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=5;j++)
{
if(!seat[i][j])
{
seat[i][j]=true;
cout<<(i-1)*5+j;
cnt++;
if(cnt==4)
{
cout<<endl;
break;
}
else
cout<<" ";
}
}
if(cnt==4)
break;
}
}
break;
}
case 5:
{
bool flag=false;
for(int i=1;i<=20;i++)
{
if(!seat[i][1] && !seat[i][2] && !seat[i][3] && !seat[i][4] && !seat[i][5])
{
seat[i][1]=seat[i][2]=seat[i][3]=seat[i][4]=seat[i][5]=true;
cout<<(i-1)*5+1<<" "<<(i-1)*5+2<<" "<<(i-1)*5+3<<" "<<(i-1)*5+4<<" "<<(i-1)*5+5<<endl;
flag=true;
break;
}
if(flag)
break;
}
if(!flag)
{
int cnt=0;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=5;j++)
{
if(!seat[i][j])
{
seat[i][j]=true;
cout<<(i-1)*5+j;
cnt++;
if(cnt==5)
{
cout<<endl;
break;
}
else
cout<<" ";
}
}
if(cnt==5)
break;
}
}
break;
}
}
}
return 0;
}