Generating 0 - 1 Vectors - Условия
Generating 0 - 1 Vectors - Условия
Examples
Input Output
3 000
001
010
011
100
101
110
111
5 00000
00001
00010
…
11110
11111
Hints
The method should receive as parameters the array which will be our vector and a current index
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
Examples
Input Output Comments Solution with nested loops
4 4 5 Console.WriteLine($"({i1} {i2}
{i3})");
4 5 5
}
5 5 5
}
}
Examples
Input Output Comments Solution with nested loops
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
4. Generating Combinations
Generate all n choose k combinations. Read the set of elements, then number of elements to choose.
Examples
Input Output
1 2 3 4 1 2
2 1 3
1 4
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
2 3
2 4
3 4
10 20 30 40 10 20 30
50 10 20 40
3 10 20 50
…
30 40 50
Hints
The method could receive the following parameters:
Loop through all possible picks for a given index of the vector:
Examples
Input Output
(no * - - - - - - -
input) - - - - * - - -
- - - - - - - *
- - - - - * - -
- - * - - - - -
- - - - - - * -
- * - - - - - -
- - - * - - - -
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
* - - - - - - -
- - - - - * - -
- - - - - - - *
- - * - - - - -
- - - - - - * -
- - - * - - - -
- * - - - - - -
- - - - * - - -
(90 solutions
more)
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
Let’s take as an example the following chessboard with 8 queens placed on it at the following positions:
{0, 0}; {1, 6}; {2, 4}; {3, 7}; {4, 1}; {5, 3}; {6, 5}; {7, 2}
Following the definitions above for our example the queen {4, 1} occupies the row 4, column 1, left diagonal -3 and
right diagonal 5.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
Recall that col - row is the number of the left diagonal and row + col is the number of the right diagonal.
On removal of a queen, we will need a method to mark as free all rows, columns and diagonals that were attacked
by it. Write it yourself:
9. Print Solutions
When a solution is found, it should be printed at the console. First, introduce a solutions counter to simplify
checking whether the found solutions are correct:
Next, pass through all rows and through all columns at each row and print the chessboard cells:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
Submit your code in judge, printing all 92 solutions, separated by a single line.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
This is one of the squares, which you should print. Magic squares with numbers from 1 to 9 inclusively are 8 in total.
They are shown below and should be printed by your program on the console like this:
If you want, you can try to find magic squares with numbers from 0 to 9 inclusively. They should be 16 in total.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
Hints
For more simplicity, you can keep squares as an array of integers. The array should have a size of 9 for all cells in the
3X3 square:
Note that you should also keep track of numbers, which have already been used in the current square, as they
should not be repeated. You may create an array, where each number has value 0 (when the number is not used) or
1 (when the number is already used). Whenever you 'go to' a recursive call and 'come back' from a recursive call,
you should update this array. The array should have a size of 9 to keep information for all numbers from 1 to 9:
Then, create a method to permute numbers. You can use the instructions from this site to help you write the
recursive algorithm: https://erwnerve.tripod.com/prog/recursion/magic.htm.
Write a TestMagic() method to check whether the current square is a magic square. It is a good idea to
generate a matrix from the square array, so you can easily check if diagonal sums, row sums and column sums are
all the same.
If a matrix is a magic square, print its numbers like a 3X3 square. Numbers should be separated by space and magic
squares should be separated by an empty line.
For checking whether a matrix is a magic square or not, you may use the following method:
static bool isMagicSquare(int[,] matrix)
{
int N = 3;
if (sum != sum2)
return false;
int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += matrix[i, j];
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Private High School for Digital Sciences "SoftUni Svetlina”
int colSum = 0;
for (int j = 0; j < N; j++)
colSum += matrix[j, i];
return true;
}
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.