美文网首页
CCF201609-2 火车购票(JAVA版)

CCF201609-2 火车购票(JAVA版)

作者: 巨鹿lx | 来源:发表于2020-03-20 19:19 被阅读0次
import java.util.Scanner;
public class Main {
    static int a[][] = new int[21][6];
    static int p[] = new int[21];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while(n-->0) {
            int x = scanner.nextInt();
            process(x);//处理每次购票请求
            System.out.println();
        }
    }
    private static void process(int u) {
        boolean flag = false;
        for(int i = 0 ;i < 20;i++) {
            if(5-p[i]>=u) {//第i行够放
                for(int j = p[i]+1;j <= p[i]+u;j++) System.out.print(i*5+j+" ");
                p[i]+=u;
                flag = true;
                break;
            }
        }
        if(!flag) {//无连续的,每一行都不能连续放
            int x = 0,y = p[x]+1;
            while(u>0) {//知道个数为零停止
                while(y==6) {//处理越界
                    x++;y = p[x]+1;
                }
                System.out.print(x*5+y+" ");
                p[x]++;y = p[x]+1;u--;
            }
        }
    }
    
}

相关文章

网友评论

      本文标题:CCF201609-2 火车购票(JAVA版)

      本文链接:https://www.haomeiwen.com/subject/sqrsyhtx.html