导读 您好,今天小编胡舒来为大家解答以上的问题。车皮刮破了需要多少钱修,车皮相信很多小伙伴还不知道,现在让我们一起来看看吧!1、OK, 按照你...

您好,今天小编胡舒来为大家解答以上的问题。车皮刮破了需要多少钱修,车皮相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、OK, 按照你说的堆栈结构, 那么就是5种调度结果。

2、加上一种没有调度的排序1,2,3. 其实它也可以看作是一种调度结果。

3、1进入调度室,又出来;2进入,又出来;3进入又出来。

4、 这个应该是P(3, 3)。

5、基本的数学问题,记得中学好像学过。

6、 你的问题是什么? 是不是想问这个调度站对于长度为n的,能够产生多少种调度结果么? =============================================================== 3 1 2这个结果是不可能的。

7、 所以结果不是简单的P(3,3)这样的排列组合问题。

8、应该是OK了,费我不少时间,你试试吧。

9、程序很多地方写得不够好,凑合用吧。

10、不想费时间去改进了。

11、 不过我没有删除重复的结果。

12、还需要你自己改进一下,小case。

13、 #define MAX_TRAIN_LEN 100 //#define PRINT_DUMP /* enable this macro to print all detail of scheduling */ typedef enum { GO_OUT_STATION = 0, GO_IN_STATION, GO_PASS_STATION } Action; static char *action_name[] = { "go out of station", "go in station", "go pass station" }; int train_len; int schedule_num; void printSchedule(int pass_train[], int pass_num) { int i; printf("----- got a schedule: "); for (i = 0; i < pass_num; i++) printf("%d, ", pass_train[i]); printf(""); } char * getActionName(Action act) { return action_name[act]; } void printTrain(char *name, int train[], int begin, int end) { int i; #ifdef PRINT_DUMP printf("%-20s: ", name); for (i = begin; i < end; i++) printf("%d, ", train[i]); printf(""); #else return; #endif } void printTrainNum(Action act, int unsched_num, int station_num, int pass_num) { #ifdef PRINT_DUMP printf("%-30s (unsched_num: %d, station_num: %d, pass_num: %d)", getActionName(act), unsched_num, station_num, pass_num); #else return; #endif } void printRestoreDump() { #ifdef PRINT_DUMP printf("restoring..."); #else return; #endif } /* Parameters: int unsched_train[MAX_TRAIN_LEN]; un-scheduled train int unsched_num; un-scheduled train number. This is the index of un-scheduled train number. So the un-scheduled trains are in range: [unsched_num, train_len-1]. int station_train[MAX_TRAIN_LEN]; train in station int station_num; train number in station, station trains are in range: [0, station_num-1] int pass_train[MAX_TRAIN_LEN]; passed train int pass_num; train number passed already, pass trains are in range: [0, pass_num-1] Action act current action */ /* * Three possible action: * 1) pass directly * 2) go into scheduling station * 3) if scheduling station has any train, go out of station */ void schedule(int unsched_train[], int unsched_num, int station_train[], int station_num, int pass_train[], int pass_num, Action act) { if (act == GO_OUT_STATION) { pass_train[pass_num++] = station_train[--station_num]; //printf("%d", pass_train[0]); printTrainNum(act, unsched_num, station_num, pass_num); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); } else if (act == GO_IN_STATION) { station_train[station_num++] = unsched_train[unsched_num++]; printTrainNum(act, unsched_num, station_num, pass_num); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); } else if (act == GO_PASS_STATION) { pass_train[pass_num++] = unsched_train[unsched_num++]; printTrainNum(act, unsched_num, station_num, pass_num); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); } if (unsched_num < train_len) { if (station_num > 0) { int old_unsched_train[MAX_TRAIN_LEN], old_station_train[MAX_TRAIN_LEN], old_pass_train[MAX_TRAIN_LEN]; int old_unsched_num, old_station_num, old_pass_num; memcpy(old_unsched_train, unsched_train, sizeof(int) * train_len); memcpy(old_station_train, station_train, sizeof(int) * train_len); memcpy(old_pass_train, pass_train, sizeof(int) * train_len); old_unsched_num = unsched_num; old_station_num = station_num; old_pass_num = pass_num; schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_OUT_STATION); printRestoreDump(); unsched_num = old_unsched_num; station_num = old_station_num; pass_num = old_pass_num; memcpy(unsched_train, old_unsched_train, sizeof(int) * train_len); memcpy(station_train, old_station_train, sizeof(int) * train_len); memcpy(pass_train, old_pass_train, sizeof(int) * train_len); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_IN_STATION); printRestoreDump(); unsched_num = old_unsched_num; station_num = old_station_num; pass_num = old_pass_num; memcpy(unsched_train, old_unsched_train, sizeof(int) * train_len); memcpy(station_train, old_station_train, sizeof(int) * train_len); memcpy(pass_train, old_pass_train, sizeof(int) * train_len); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_PASS_STATION); } else { int old_unsched_train[MAX_TRAIN_LEN], old_station_train[MAX_TRAIN_LEN], old_pass_train[MAX_TRAIN_LEN]; int old_unsched_num, old_station_num, old_pass_num; old_unsched_num = unsched_num; old_station_num = station_num; old_pass_num = pass_num; memcpy(old_unsched_train, unsched_train, sizeof(int) * train_len); memcpy(old_station_train, station_train, sizeof(int) * train_len); memcpy(old_pass_train, pass_train, sizeof(int) * train_len); schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_IN_STATION); printRestoreDump(); unsched_num = old_unsched_num; station_num = old_station_num; pass_num = old_pass_num; memcpy(unsched_train, old_unsched_train, sizeof(int) * train_len); memcpy(station_train, old_station_train, sizeof(int) * train_len); memcpy(pass_train, old_pass_train, sizeof(int) * train_len); printTrain("unsched", unsched_train, unsched_num, train_len); printTrain("station", station_train, 0, station_num); printTrain("pass", pass_train, 0, pass_num); schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_PASS_STATION); } } else if (station_num > 0) { schedule(unsched_train, unsched_num, station_train, station_num, pass_train, pass_num, GO_OUT_STATION); } else { /* get a schedule */ if (pass_num != train_len) { printf("Error: pass_num is not equal to train_len."); exit(1); } schedule_num++; printSchedule(pass_train, pass_num); } } void main() { int unsched_train[MAX_TRAIN_LEN]; int station_train[MAX_TRAIN_LEN]; int pass_train[MAX_TRAIN_LEN]; int i; printf("Please input train length: "); scanf("%d", &train_len); printf("=================================================="); if (train_len > MAX_TRAIN_LEN) { printf("train length is too large, maximal length is %d.", MAX_TRAIN_LEN); exit(1); } for (i = 0; i < train_len; i++) unsched_train[i] = i + 1; schedule_num = 0; schedule(unsched_train, 0, station_train, 0, pass_train, 0, GO_IN_STATION); printf("=================================================="); printf("Total schedule number is: %d", schedule_num); }。

本文就为大家分享到这里,希望小伙伴们会喜欢。