CCF|火车购票|Java|80分

时间:2020-11-28 21:34:55

import java.util.*;

public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] ints = new int[20][5];

    for (int i = 0; i < 20; i++)
    {
        Arrays.fill(ints[i], 0);
    }

    int si = 0;
    for (int j = 0; j < n; j++)
    {
        int site = in.nextInt();

        for (int i = 0; i < 20; i++)
        {

            if (si <= 19)
            {
                if (site <= foundPlace(ints[i]))
                {
                    setPlace(ints[i], i, site);
                    if (si <= i)
                    {
                        si++;
                    }
                    break;
                }
            }
            else if (si > 19)
            {
                if (site <= foundPlace(ints[i]))
                {
                    setPlace(ints[i], i, site);
                    return;
                }
                else if (foundPlace(ints[i]) < site)
                {
                    int s = foundPlace(ints[i]);
                    setPlace(ints[i], i, foundPlace(ints[i]));
                    site -= s;

                }
            }
        }
    }
}

/**
 * @param ints
 * @return 空位置数目
 */
private static int foundPlace(int[] ints)
{
    int s = 0;
    for (int i = 4; i >= 0 && ints[i] == 0; i--)
    {
        s++;
    }
    return s;
}

/**
 * @param ints
 * @return
 */
private static void setPlace(int[] ints, int p, int q)
{
    int qq = 0;
    for (int i = 0; i < 5; i++)
    {
        if (ints[i] == 0 && qq < q)
        {
            ints[i] = p * 5 + i + 1;
            qq++;

            System.out.print(ints[i]);
            if (qq != q)
            {
                System.out.print(" ");
            }
            else if (qq == q)
            {
                System.out.println();
            }
        }
    }
}

}