1 coin flip
There is a row of coins on the desktop, N in total, each coin is positive up. Now I want to flip all the coins to the opposite side up, the rule is to flip any N-1 each time (the front is turned to the opposite side up, and vice versa ).
Find a shortest operation sequence (turning each N-1 coin into an operation.
Input:
There is only one row, containing a natural number n (n is not equal to an even number of 100 ).
Output:
The first row contains an integer "S", indicating the minimum number of operations required. Each row in the next s line represents the status of the coin after each operation (one row contains N integers 0 or 1), indicating the status of each coin. 0-front up, and 1-back up, no extra space is allowed.
For a variety of operating solutions, only one is output.
Example:
Input
4
Output
4
0111
1100
0001
1111
// If you want to practice the BFS question, it is also good, but it is best to do it in a mathematical way // BFs, convert the state to binary, 0 indicates that positive 1 indicates that reverse // changes all 1 values to 0 for each extension, changes all 0 values to 1, and then cyclically selects one and changes it back .. I personally think this is a good operation. // Then design a hash weight. // the final result will be over. // The minimum number of steps is the same, but the steps printed by different methods are different // The following is a mathematical method, which is not very nice but very simple/* 1st times, except 1st, all others are turned over. 2nd times, except 2nd, all others are turned over ....... The n times, except the n times, all the others are turned over. In this way, each coin is flipped over n-1 times. N-1 is an odd number because N is an even number. The coin turned up an odd number of times, so it fell down. */# Include <stdio. h> int main () {int N, I, j, a [101] = {-1}, k = 1; scanf ("% d", & N ); for (I = 1; I <= N; I ++) A [I] = 0; printf ("% d \ n", n); for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {If (j = K) {A [J] = A [J];} else {if (a [J] = 1) {A [J] = 0 ;} else if (a [J] = 0) {A [J] = 1 ;}}for (j = 1; j <= N; j ++) printf ("% d", a [J]); printf ("\ n"); k ++;} return 0 ;}