1. 列车厢调动 1.1 实验内容 有三条平行的列车轨道(1、2、3)以及 1-3 和 2-3 两段连接轨道。现有一列车厢停在1号轨道上,请利用两条连接轨道以及3号轨道,将车厢按照要求的顺序转移到2号轨道。
1.2 实验目的 掌握栈的基本操作,入栈和出栈,能够应用栈来解决问题。
1.3 程序清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define MAX_CARS 100 typedef struct stack_s { char data[MAX_CARS]; int top; } stack_t ;void init_stack (stack_t *s) { s->top = -1 ; }bool is_empty (stack_t *s) { return s->top == -1 ; }bool is_full (stack_t *s) { return s->top == MAX_CARS - 1 ; }bool push (stack_t *s, int data) { if (is_full(s)) { return false ; } s->data[++(s->top)] = data; return true ; }bool pop (stack_t *s, int *data) { if (is_empty(s)) { return false ; } *data = s->data[(s->top)--]; return true ; }bool peek (stack_t *s, int *data) { if (is_empty(s)) { return false ; } *data = s->data[s->top]; return true ; }void train_shunting (int n, char source[], char target[]) { stack_t track; init_stack(&track); int source_idx = 0 ; int target_idx = 0 ; for (target_idx = 0 ; target_idx < n; target_idx++) { int expected_car = target[target_idx]; int top_car; while (1 ) { if (!is_empty(&track) && peek(&track, &top_car) && top_car == expected_car) { pop(&track, &top_car); printf ("3->2\n" ); break ; } else if (source_idx < n && source[source_idx] == expected_car) { printf ("1->2\n" ); source_idx++; break ; } else if (source_idx < n) { push(&track, source[source_idx]); printf ("1->3\n" ); source_idx++; } else { printf ("ERROR!\r\n" ); return ; } } } }int main () { char source[MAX_CARS + 1 ]; char target[MAX_CARS + 1 ]; if (scanf ("%s" , &source) != 1 || scanf ("%s" , target) != 1 ) { printf ("Invalid input!\r\n" ); return -1 ; } int n = strlen (source); if (n == 0 || n > MAX_CARS) { return -1 ; } train_shunting(n, source, target); return 0 ; }
1.4 调试步骤
输入测试数据
程序输出
1 2 3 4 5 1->3 1->3 1->2 3->2 3->2
1.5 分析与思考
空间复杂度:$O(n)$
时间复杂度:$O(n)$
2. 银行业务队列简单模拟 2.1 实验内容 设某银行有 A、B 两个业务窗口,且处理业务的速度不一样,其中 A 窗口处理速度是 B 窗口的2倍,即当 A 窗口每处理完2个顾客时,B 窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A 窗口顾客优先输出。
输入给出顾客总数和每个顾客的编号。按照编号奇偶分别进入 A 窗口和 B 窗口排队。
2.2 实验目的 掌握队列的基本操作,入队和出队,能够应用队列来解决问题。
2.3 程序清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX 1005 typedef struct queue_s { int data[MAX]; int front; int rear; } queue_t ;void init (queue_t *q) { q->front = q->rear = 0 ; }bool is_empty (queue_t *q) { return q->front == q->rear; }void enqueue (queue_t *q, int x) { q->data[q->rear++] = x; }int dequeue (queue_t *q) { return q->data[q->front++]; }int main () { int n; int num; queue_t qA; queue_t qB; init(&qA); init(&qB); scanf ("%d" , &n); for (int i = 0 ; i < n; i++) { scanf ("%d" , &num); if (num % 2 ) { enqueue(&qA, num); } else { enqueue(&qB, num); } } int first = 1 ; while (!is_empty(&qA) || !is_empty(&qB)) { if (!is_empty(&qA)) { if (!first) { printf (" " ); } printf ("%d" , dequeue(&qA)); first = 0 ; if (!is_empty(&qA)) { printf (" %d" , dequeue(&qA)); } } if (!is_empty(&qB)) { if (!first) { printf (" " ); } printf ("%d" , dequeue(&qB)); first = 0 ; } } printf ("\r\n" ); return 0 ; }
2.4 调试步骤
输入测试数据
程序输出
2.5 分析与思考
空间复杂度:$O(n)$
时间复杂度:$O(n)$