问题描述
思路
由于始终都是编号较小的座位优先被使用,因此不会出现下图这种间隔的情况
0 | 0 | 1 | 0 | 0 |
---|
也即座位只会是这样的:
1 | 1 | 1 | 0 | 0 |
---|
因此
case1:可以安排相邻座位
只需要判断在该行中是否有空座的数量 == 某人购票数量就可以了
若满足该条件,则依次将该座位标志置为1(表示该座位有人了),并输出对应的座位号
case2:没有相邻座位可以安排
当100个座位遍历完毕使,仍有某行空座的数量 != 某人购票数量,说明只能安排较小编号的座位见缝插针买票
此时只需要从头遍历,选取seat[ j ] == 0的座位售卖。
完整代码
#include<iostream>
using namespace std;
int seat[101] = {0};
int main(){
int n;
cin >> n;
int a[n];
for( int i = 0; i < n; i++)
cin >> a[i];
int sum = 0;
for( int i = 0; i < n; i++){
for( int j = 0; j < 100; j++){
if( j%5 == 0)
sum = 0;
if( seat[j] == 0)
sum++;
if( sum == a[i]){ //可以连坐
j = j - sum + 1;
for( int counter = sum; counter > 0; counter--)
{
seat[j] = 1;
cout << j + 1 << ' ';
j++;
}
cout << endl;
break;
}
}
if( sum != a[i]){ //没有连坐
for( int j = 0; j < 100; j++){
if( seat[j] == 0 && a[i] > 0){
seat[j] = 1;
cout << j+1 << ' ';
a[i]--;
}
if( a[i] == 0)
break;
}
cout << endl;
}
}
return 0;
}