Article Directory
Topic link .
Algorithm idea
Using a linear table, an array is used here. Because the seats in the same row are to be judged for the first time, an additional array place
is set to record the number of remaining seats in each row, so that you don't need to traverse each seat to get the result. For the second judgment, you have to traverse from the beginning and find the required quantity.
#include<iostream>
using namespace std;
//数组
int chair[21][6] = {};//座位编号,0 代表售出
int place[21] = {}; //每一排剩余座位数
int n, p, ticket = 0; //ticket 出票数
bool done = false; //出售成功
int main()
{
int i, j, k;
for ( i = 1; i <= 20; i++)
{
place[i] = 5;
for ( j = 1; j <= 5; j++)
{
chair[i][j] = (i - 1) * 5 + j;
}
}
scanf("%d", &n);
for ( k = 1; k <= n; k++)
{
scanf("%d", &p);
done = false;
for ( i = 1; i <= 20; i++)
{
if (p <= place[i])
{
for ( j = 1; j <= p; j++)
{
printf("%d ", chair[i][5 - place[i] + j]);
//chair[i][j] = 0;
}
place[i] -= p;
done = true;
break;
}
}
if (!done) //没有连续的票
{
ticket = 0;
for (i = 1; i <= 20 && ticket < p; i++)
{
for (j = 6 - place[i]; j <= 5 && ticket < p; j++)
{
printf("%d ", chair[i][j]);
place[i]--;
ticket++;
}
}
}
printf("\n");
}
return 0;
}
Result analysis
The score is 100 points, the time is 0ms, the space is 2.937MB, and the time complexity is O©.