# UVa Online Judge 402 - M*A*S*H [402 - M*A*S*H](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=343) ## Solution ```cpp= #define WINDOWS 0 #include <cstdio> #include <cstdlib> #include <cstring> const int MAX_DECK_NUM = 20; const int MAX_PEOPLE_NUM = 50; int people_array[MAX_PEOPLE_NUM]; int deck_stack[MAX_DECK_NUM]; void Lottery(const int &N, const int &X, const int &time); void Init(const int &N); int main(int argc, char *argv[]) { int N; int X; int time = 1; int i; char input_buffer[100]; char *token; /* while (fgets(input_buffer, 99, stdin)) { token = strtok(input_buffer, " "); N = atoi(token); token = strtok(NULL, " "); X = atoi(token); for (i = 0; i < MAX_DECK_NUM; ++i) { token = strtok(NULL, " "); deck_stack[i] = atoi(token); } Lottery(N, X, time); ++time; } */ while (scanf("%d %d", &N, &X) != EOF) { int *deck_pointer = &deck_stack[0]; for (i = 0; i < MAX_DECK_NUM; ++i) { //scanf("%d", &deck_stack[i]); scanf("%d", deck_pointer++); } Lottery(N, X, time++); //++time; } #if WINDOWS system("pause"); #endif return 0; } void Lottery(const int &N, const int &X, const int &time) { int count = 0; int pos = 0; int remain = N; int i; Init(N); while (remain > X && count < MAX_DECK_NUM) { pos += deck_stack[count]-1; while (pos >= remain && count < MAX_DECK_NUM) { ++count; pos = deck_stack[count]-1; } if (count >= MAX_DECK_NUM || pos >= remain) { break; } --remain; for (i = pos; i < remain; ++i) { people_array[i] = people_array[i+1]; } } printf("Selection #%d\n", time); for (i = 0; i < remain-1; ++i) { printf("%d ", people_array[i]); } printf("%d", people_array[i]); puts("\n"); } void Init(const int &N) { int i; for (i = 0; i < N; ++i) { people_array[i] = i+1; } } ``` --- ## Result ![](https://i.imgur.com/qKxSuZ9.png) ###### tags: `UVa` `C++`