给定一个整数N ,任务是构造一个大小为N * N的二进制矩阵,以使矩阵的每一行和每一列的和为素数。
例子:
Input: N = 2
Output:
Explanation:
Sum of 0th row = 1 + 1 = 2 (Prime number)
Sum of 1st row = 1 + 1 = 2 (Prime number)
Sum of 0th column = 1 + 1 = 2 (Prime number)
Sum of 1st column = 1 + 1 = 2 (Prime number)
Input: N = 4
Output:
方法:请按照以下步骤解决问题:
- 初始化一个二进制矩阵,例如mat [] [] ,大小为N * N。
- 将mat [i] [i]的所有可能值更新为1 。
- 将mat [i] [N – i -1]的所有可能值更新为1 。
- 如果N为奇数,则将值mat [N / 2] [0]和mat [0] [N / 2]更新为1 。
下面是上述方法的实现。
C++
1 1
1 1
Java
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Python3
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to construct
// the required binary matrix
void constructBinaryMat(int N)
{
// Stores binary value with row
// and column sum as prime number
int mat[N][N];
// initialize the binary matrix mat[][]
memset(mat, 0, sizeof(mat));
// Update all possible values of
// mat[i][i] to 1
for (int i = 0; i < N; i++) {
mat[i][i] = 1;
}
// Update all possible values of
// mat[i][N - i -1]
for (int i = 0; i < N; i++) {
mat[i][N - i - 1] = 1;
}
// Check if N is an odd number
if (N % 2 != 0) {
// Update mat[N / 2][0] to 1
mat[N / 2][0] = 1;
// Update mat[0][N / 2] to 1
mat[0][N / 2] = 1;
}
// Print required binary matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
int N = 5;
constructBinaryMat(N);
return 0;
}
C#
// Java program to implement
// the above approach
class GFG{
// Function to construct
// the required binary matrix
static void constructBinaryMat(int N)
{
// Stores binary value with row
// and column sum as prime number
int mat[][] = new int[N][N];
// Update all possible values
// of mat[i][i] to 1
for (int i = 0; i < N; i++)
{
mat[i][i] = 1;
}
// Update all possible values
// of mat[i][N - i -1]
for (int i = 0; i < N; i++)
{
mat[i][N - i - 1] = 1;
}
// Check if N is an odd
// number
if (N % 2 != 0)
{
// Update mat[N / 2][0]
// to 1
mat[N / 2][0] = 1;
// Update mat[0][N / 2]
// to 1
mat[0][N / 2] = 1;
}
// Print required binary matrix
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
constructBinaryMat(N);
}
}
// This code is contributed by Chitranayal
输出:
# Python3 program to implement
# the above approach
# Function to construct
# the required binary matrix
def constructBinaryMat(N):
# Stores binary value with row
# and column sum as prime number
mat = [[0 for i in range(N)]
for i in range(N)]
# Initialize the binary matrix mat[][]
# memset(mat, 0, sizeof(mat));
# Update all possible values of
# mat[i][i] to 1
for i in range(N):
mat[i][i] = 1
# Update all possible values of
# mat[i][N - i -1]
for i in range(N):
mat[i][N - i - 1] = 1
# Check if N is an odd number
if (N % 2 != 0):
# Update mat[N / 2][0] to 1
mat[N // 2][0] = 1
# Update mat[0][N / 2] to 1
mat[0][N // 2] = 1
# Print required binary matrix
for i in range(N):
for j in range(N):
print(mat[i][j], end = " ")
print()
# Driver Code
if __name__ == '__main__':
N = 5
constructBinaryMat(N)
# This code is contributed by mohit kumar 29
时间复杂度: O(N 2 )
辅助空间: O(N 2 )