201609-2 | |
Question name: | Train ticket |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description Please implement a simple seat allocation algorithm of a railway ticketing system to handle the seat allocation of a carriage. Input format The first line of input contains an integer n , which represents the number of ticket purchase instructions. Output format Output n lines, each line corresponds to the processing result of an instruction. Sample input 4 Sample output 1 2 Sample description 1) Purchase 2 tickets and get seats 1 and 2. Evaluation use case scale and conventions For all evaluation use cases, 1 ≤ n ≤ 100, and the sum of all tickets purchased does not exceed 100. |
import java.util.Scanner;
public class 火车购票 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=sc.nextInt();
}
int[][] pos=new int[20][5];
for(int i=1;i<=n;i++){
int mark=-1;
int count=0;
for(int x=0;x<20;x++){
for(int y=0;y<5;y++){
if(pos[x][y]==0&&a[i]<=5){
count++;
if(count==a[i]){
mark=y+1-a[i];
for(int r=0;r<a[i];r++){
pos[x][mark+r]=1;
System.out.print(5*x+mark+1+r+" ");
}
System.out.println();
break;
}
}
}
count=0;
if(mark!=-1){
break;
}else{
count=0;
continue;
}
}
if(mark==-1){
count=0;
for(int k=0;k<20;k++){
for(int j=0;j<5;j++){
if(pos[k][j]==0){
count++;
pos[k][j]=1;
System.out.print(k*5+(j+1)+" ");
if(count==a[i]){
System.out.println();
break;
}
}
}
if(count==a[i]){
break;
}else{
continue;
}
}
}
}
}
}