给定一个数字N ,任务是创建一个大小为N*N且值在 [1, N*N] 范围内的方阵,使得偶数子方阵的每条对角线之和为偶数。
例子:
Input: N = 3
Output:
1 2 3
4 5 6
7 8 9
Explanation:For each even sub-square matrix the sum of each diagonal is a even number.
1 2
4 5
sum of each diagonal is 6 and 6 i.e even number.
Input: N = 4
Output:
1 2 3 4
6 5 8 7
9 10 11 12
14 13 16 15
Explanation:
For each even sub-square matrix the sum of each diagonal is a even number.
1 2
6 5
sum of each diagonal is 6 and 8 i.e even number.
方法:这个想法是按照以下给定的方式排列从1 到 N*N 的元素:
- 分别用 1 和 2 个元素初始化奇数和偶数。
- 在[0, N]范围内迭代两个嵌套循环。
- 如果两个嵌套循环指标的总和是偶数的打印奇数和增量奇数由2并且如果该总和是奇数然后打印的偶数的值,增量甚至由2的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print N*N order matrix
// with all sub-matrix of even order
// is sum of its diagonal also even
void evenSubMatrix(int N)
{
// Even index
int even = 1;
// Odd index
int odd = 2;
// Iterate two nested loop
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// For even index the element
// should be consecutive odd
if ((i + j) % 2 == 0) {
cout << even << " ";
even += 2;
}
// for odd index the element
// should be consecutive even
else {
cout << odd << " ";
odd += 2;
}
}
cout << "\n";
}
}
// Driver Code
int main()
{
// Given order of matrix
int N = 4;
// Function call
evenSubMatrix(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print N*N order matrix
// with all sub-matrix of even order
// is sum of its diagonal also even
static void evenSubMatrix(int N)
{
// Even index
int even = 1;
// Odd index
int odd = 2;
// Iterate two nested loop
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// For even index the element
// should be consecutive odd
if ((i + j) % 2 == 0)
{
System.out.print(even + " ");
even += 2;
}
// For odd index the element
// should be consecutive even
else
{
System.out.print(odd + " ");
odd += 2;
}
}
System.out.println();
}
}
// Driver code
public static void main(String[] args)
{
// Given order of matrix
int N = 4;
// Function call
evenSubMatrix(N);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to prN*N order matrix
# with all sub-matrix of even order
# is sum of its diagonal also even
def evenSubMatrix(N):
# Even index
even = 1
# Odd index
odd = 2
# Iterate two nested loop
for i in range(N):
for j in range(N):
# For even index the element
# should be consecutive odd
if ((i + j) % 2 == 0):
print(even, end = " ")
even += 2
# For odd index the element
# should be consecutive even
else:
print(odd, end = " ")
odd += 2
print()
# Driver Code
# Given order of matrix
N = 4
# Function call
evenSubMatrix(N)
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Function to print N*N order matrix
// with all sub-matrix of even order
// is sum of its diagonal also even
static void evenSubMatrix(int N)
{
// Even index
int even = 1;
// Odd index
int odd = 2;
// Iterate two nested loop
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// For even index the element
// should be consecutive odd
if ((i + j) % 2 == 0)
{
Console.Write(even + " ");
even += 2;
}
// For odd index the element
// should be consecutive even
else
{
Console.Write(odd + " ");
odd += 2;
}
}
Console.WriteLine();
}
}
// Driver code
public static void Main(String[] args)
{
// Given order of matrix
int N = 4;
// Function call
evenSubMatrix(N);
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
1 2 3 4
6 5 8 7
9 10 11 12
14 13 16 15
时间复杂度: O(N*N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live