← 返回 amazon 的题目列表Student Printing Queue: Enumerate All Valid Printing Orders (Queue to Ring Variant)
类型:online_judge
Student Printing Queue: Enumerate All Valid Completion Orders (Queue + Ring Variant)
There are N students labeled 1..N initially in a queue.
Rules:
If the front student chooses to print, they finish printing and leave the queue.
If the front student chooses not to print, they move to the back of the queue.
Repeat until all students have printed and left.
Task 1
Given N, output all possible printing completion orders (each is a permutation of length N). You may output in lexicographic order.
Task 2 (Follow-up: queue becomes a ring)
Treat students as arranged in a circle with a moving pointer.
The pointer starts at student 1.
At each step you may:
Print and remove the student at the pointer, then move the pointer to the next remaining student.
Skip printing and move the pointer to the next student.
Continue until all students are removed.
Output all possible completion orders.
Input
A single integer N
Output
Print each possible completion order on its own line, space-separated.
Constraints
1 <= N <= 8
Example
Input:
3
Output (format example; order of lines not important):
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Note: this example only illustrates output format; the actual set should match the rules.
Example
Input
1
Output
1