Basic Data Structure of the queue ~
Enter a positive integer N, representing N soldiers numbered 1-N. Start to report the number. First, 1212 reports are reported to 2, and then 123123 reports to 3; then there are another 1212 reports until there are only three people whose numbers are less than or equal.
Example:
Input: 20
Output: 1 7 19
Code:
1 #include <iostream> 2 using namespace std; 3 int que[10001] = { 0 }; 4 int main() 5 { 6 int n; 7 cin >> n; 8 int head = 1, tail = 1; 9 for (int i = 1; i <= n; i++) que[tail++] = i;10 while (1)11 {12 n = tail - head;13 if (tail - head <= 3)break;14 for (int i = 1; i <= n; i++)15 {16 if (i % 2 == 1)que[tail++] = que[head];17 head++;18 }19 n = tail - head;20 if (tail - head <= 3)break;21 for (int i = 1; i <= n; i++)22 {23 if (i % 3 != 0)que[tail++] = que[head];24 head++;25 }26 }27 for (int i = head; i < tail; i++)28 {29 cout << que[i] << " ";30 }31 }
Result:
Number of soldiers in the queue