preface
The author searched for a long time and didn't find the right answer, so he wrote a c + + program to traverse all the possibilities
The title and code are as follows
1, Title
Once upon a time, there was a rich man who had 30 children, of which 15 were born to his late ex-wife and the other 15 were born to his stepwife. The latter woman wanted her eldest son to inherit the property, so one day, He said to him, "dear husband, you are getting old. We should decide who will be your heir. Let's line up our 30 children in a circle. From one of them, let the child stand out every 10, until which child is left and which child inherits your property!" The rich man thought, shit, the meaning of this question is quite meaningful. Yes, it seems very fair. Let's do it like this ~ however, when the selection process continues, the rich man was stupid. He found that the first 14 children to be removed were born by his ex-wife, and the next one to be removed was born by his ex-wife. The rich man immediately waved his hand, stopped, and now count back from this child and step into the room, It was this vicious stepmother who counted down when she thought about it. My 15 sons couldn't fight you. She immediately agreed to the rich man's motion. Guess who became the successor in the end?
2, Train of thought
Use an array to store numbers from 1 to 30 for elimination. Who is eliminated, the array element becomes 0, and assign the original value of this array element to another array b[15]. This array is used to finally output which children are eliminated, and then traverse in reverse:
The codes are as follows:
#include <iostream> using namespace std; int main () { //30 numbers in the array, 30 to 30 numbers in the array, respectively int a[30]; for (int i = 0; i < 30; i++) a[i] = i + 1; //The definition array b[15] is used to store the number of the rejected person int b[15]; for (int i = 0; i < 15; i++) b[i] = 0; int n = 0; //Used to count the b array //The first stage is positive elimination //Requirements: after elimination, the corresponding value of the array element is 0. When counting, first judge whether the array element is 0. If it is 0, it will not be counted. If it is not 0, it will be counted. After the subscript of the array reaches 29, start with the subscript of 0 int j = 0; //j is used to record several non-zero a array elements for (int i = 0;;i++) //i is traversal from 1 to 30 { //For each cycle, it is necessary to judge whether the flag variable of the cycle is equal to 30. If it is equal to 30, it means that this is the case of i=0. Traverse from the beginning if (i == 30) i -= 30; //Judge whether the value of the array is 0. If not, i+1 if (a[i] != 0) { j += 1; //First, check whether j needs + 1. If not, the array element has been eliminated, so there is no need to judge whether it needs to be eliminated. if (j == 10 ) { n += 1; //Represents one more of the number eliminated b[n - 1] = a[i]; //Used to record which element of array a is eliminated a[i] = 0; j -= 10; } } //Flag for the end of forward traversal if (n == 15) break; } //The forward traversal has ended, and all the sons of the first 14 excluded ex wives have been selected for (int i = 0; i < 14; i++) { cout << b[i] << " "; if (i % 4 == 0&&i!=0) cout << endl; } //Restore the corresponding of b[14] int t = b[14]; a[t] = t; //Next, reverse traversal is performed int c[15]; for (int i = 0; i < 15; i++) c[i] = 0; //The function of c is a reduced version of A j = 0, n = 0; //Because a new round of traversal is carried out at this time for (int i = b[14] - 1;; i--) { if (i < 0) break; if (a[i] != 0) { j += 1; //First, check whether j needs + 1. If not, the array element has been eliminated, so there is no need to judge whether it needs to be eliminated. if (j == 10) { n += 1; //Represents one more of the number eliminated c[n - 1] = a[i]; //Used to record which element of array a is eliminated a[i] = 0; j -= 10; } } } //At this point, the reverse traversal ends //Then forward traversal is performed for (int i = 1;; i++) //i is traversal from 1 to 30 { //For each cycle, it is necessary to judge whether the flag variable of the cycle is equal to 30. If it is equal to 30, it means that this is the case of i=0. Traverse from the beginning if (i == 30) i -= 30; //Judge whether the value of the array is 0. If not, i+1 if (a[i] != 0) { j += 1; //First, check whether j needs + 1. If not, the array element has been eliminated, so there is no need to judge whether it needs to be eliminated. if (j == 10) { n += 1; //Represents one more of the number eliminated c[n - 1] = a[i]; //Used to record which element of array a is eliminated a[i] = 0; j -= 10; } } if (n == 15) break; } for (int i = 0; i < 15; i++) { cout << c[i] << " "; if (i % 4 == 0 && i != 0) cout << endl; } cout << endl << endl; for (int i = 0; i < 30; i++) if (a[i] != 0) cout << a[i]; return 0; }
Because I am a beginner and the code quality is relatively low, I only provide a general idea.