试题编号: | 201609-2 |
试题名称: | 火车购票 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配。 输入格式 输入的第一行包含一个整数n,表示购票指令的数量。 输出格式 输出n行,每行对应一条指令的处理结果。 样例输入 4 样例输出 1 2 样例说明 1) 购2张票,得到座位1、2。 评测用例规模与约定 对于所有评测用例,1 ≤ n ≤ 100,所有购票数量之和不超过100。 |
#include<bits/stdc++.h>
using namespace std;
struct Carriage{
int row;
int seat[6]={0};
};
int main()
{
Carriage c[21];
int n,a[101];
cin>>n;
for(int i=0;i<20;i++)
{
c[i].row=5;
}
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]>5)
{
for(int j=0;j<20&&a[i];j++)
{
for(int k=0;k<5&&a[i];k++)
{
if(c[j].seat[k]==0)
{
cout<<(j)*5+k+1<<" ";
c[j].row-=1;
c[j].seat[k]=1;
a[i]--;
}
}
}
cout<<endl;
}
else
{
int count=0;
for(int j=0;j<20&&a[i];j++)
{
if(a[i]<=c[j].row)
{
for(int k=0;k<5&&a[i];k++)
{
if(c[j].seat[k]==0)
{
cout<<(j)*5+k+1<<" ";
c[j].row-=1;
c[j].seat[k]=1;
a[i]--;
}
}
cout<<endl;
}
else
count++;
}
if(count==20)
{
for(int j=0;j<20&&a[i];j++)
{
for(int k=0;k<5&&a[i];k++)
{
if(c[j].seat[k]==0)
{
cout<<(j)*5+k+1<<" ";
c[j].row-=1;
c[j].seat[k]=1;
a[i]--;
}
}
}
cout<<endl;
}
}
}
return 0;
}