提高题

This commit is contained in:
iridiumR 2021-11-18 21:42:21 +08:00
parent fad43a0246
commit e2f36ca41b

View file

@ -1,13 +1,13 @@
#include "queue.hpp" #include "queue.hpp"
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#define JIE 6 #define JIE 10
class Train class Train
{ {
private: private:
int _v[6]; int _v[10];
Queue<int> come; Queue<int> come[2];
Queue<int> temp[4]; Queue<int> temp[4];
Queue<int> out; Queue<int> out;
@ -21,10 +21,18 @@ public:
for (int i = 0; i < JIE; i++) for (int i = 0; i < JIE; i++)
swap(i, rand() % JIE); swap(i, rand() % JIE);
printf("入队序列: \n"); printf("入队序列: \n");
for (int i = 0; i < JIE; i++) printf("#1到达轨 ");
for (int i = 0; i < 5; i++)
{ {
come.enqueue(_v[i]); come[0].enqueue(_v[i]);
printf("%d ", _v[i]); printf("%2d ", _v[i]);
}
printf("\n");
printf("#2到达轨 ");
for (int i = 5; i < 10; i++)
{
come[1].enqueue(_v[i]);
printf("%2d ", _v[i]);
} }
printf("\n"); printf("\n");
} }
@ -32,20 +40,37 @@ public:
bool orgnize() bool orgnize()
{ {
int j = 0; int j = 0;
while (!come.empty()) while (!come[1].empty() || !come[0].empty())
{ {
for (int i = 0; i < 4;) for (int i = 0; i < 4;)
{ {
if (temp[i].back() < come.front()) if (temp[i].back() <
(come[1].front() < come[0].front() ?
(come[1].front() ? come[1].front() : come[0].front()) :
(come[0].front() ? come[0].front() : come[1].front())))
{ {
printf("%d号车厢推入#%d缓冲轨 \n", come.front(), i + 1); printf("%2d号车厢由#%d到达轨推入#%d缓冲轨 \n",
temp[i].enqueue(come.dequeue());
(come[1].front() < come[0].front() ?
(come[1].front() ? come[1].front() : come[0].front()) :
(come[0].front() ? come[0].front() : come[1].front())),
(come[1].front() < come[0].front() ?
(come[1].front() ? 2 : 1) :
(come[0].front() ? 1 : 2)),
i + 1);
temp[i].enqueue(
(come[1].front() < come[0].front() ?
(come[1].front() ? come[1].dequeue() : come[0].dequeue()) :
(come[0].front() ? come[0].dequeue() : come[1].dequeue())));
} }
else else
{ {
j++; j++;
i++; i++;
if (j > 20) if (j > 100)
return false; return false;
} }
} }
@ -60,7 +85,7 @@ public:
{ {
if (temp[j].front() == i) if (temp[j].front() == i)
{ {
printf("%d号车厢经#%d缓冲轨推入出发区出发队列为 ", j+1,temp[j].front()); printf("%2d号车厢经#%d缓冲轨推入出发区出发队列为 ", temp[j].front(), j + 1);
out.enqueue(temp[j].dequeue()); out.enqueue(temp[j].dequeue());
for (int i = 0; i < out.size(); i++) for (int i = 0; i < out.size(); i++)
printf("%d ", out[i]); printf("%d ", out[i]);
@ -73,8 +98,6 @@ public:
return 0; return 0;
} }
protected: protected:
void swap(int a, int b) void swap(int a, int b)
{ {
@ -86,7 +109,6 @@ protected:
int main() int main()
{ {
Train T; Train T;
T.orgnize() ? T.Out() : printf("编组失败\n"); T.orgnize() ? T.Out() : printf("编组失败\n");
return 0; return 0;