矩阵中通过(x,y)的左右对角线的单元格数
给定四个整数row、col、x 和 y ,其中row 和 col是二维矩阵的行数和列数, x 和 y是同一矩阵中单元格的坐标,任务是找到单元格的数量在与矩阵的单元格 (x, y) 相关联的左右对角线上。
例子:
Input: row = 4, col = 3, x = 2, y = 2
Output: 3 3
The number of cells in the left and the right diagonals of (2, 2) are 3 and 3 respectively.
Input: row = 4, col = 5, x = 2, y = 2
Output: 4 3
方法:
- 分别计算单元格(x,y)左对角线左上部分和右下部分的单元格数。然后将它们相加以获得左对角线上的单元格数量。
- 同理,分别计算单元格(x,y)右对角线右上部分和左下部分的单元格数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the number of cells
// in the left and the right diagonal of
// the matrix for a cell (x, y)
void count_left_right(int n, int m, int x, int y)
{
int left = 0, right = 0;
// number of cells in the left diagonal
int left_upper_part = min(x-1, y-1);
int left_lower_part = min(n-x, m-y);
left = left_upper_part + left_lower_part + 1;
// number of cells in the right diagonal
int right_upper_part = min(m-y, x-1);
int right_lower_part = min(y-1, n-x);
right = right_upper_part + right_lower_part + 1;
cout<<(left)<<" "<<(right);
}
// Driver code
int main()
{
int row = 4;
int col = 3;
int x = 2;
int y = 2;
count_left_right(row, col, x, y);
}
// This code is contributed by
// Sanjit_Prasad
Java
// Java implementation of the approach
class GFG {
// Function to return the number of cells
// in the left and the right diagonal of
// the matrix for a cell (x, y)
static void count_left_right(int n, int m
, int x, int y)
{
int left = 0, right = 0;
// number of cells in the left diagonal
int left_upper_part = Math.min(x - 1, y - 1);
int left_lower_part = Math.min(n - x, m - y);
left = left_upper_part + left_lower_part + 1;
// number of cells in the right diagonal
int right_upper_part = Math.min(m - y, x - 1);
int right_lower_part = Math.min(y - 1, n - x);
right = right_upper_part + right_lower_part + 1;
System.out.println(left + " " + right);
}
// Driver code
public static void main(String[] args)
{
int row = 4;
int col = 3;
int x = 2;
int y = 2;
count_left_right(row, col, x, y);
}
}
Python 3
# Python 3 implementation of the approach
# Function to return the number of cells
# in the left and the right diagonal of
# the matrix for a cell (x, y)
def count_left_right(n, m, x, y):
left = 0
right = 0
# number of cells in the left diagonal
left_upper_part = min(x - 1, y - 1)
left_lower_part = min(n - x, m - y)
left = left_upper_part + left_lower_part + 1
# number of cells in the right diagonal
right_upper_part = min(m - y, x - 1)
right_lower_part = min(y - 1, n - x)
right = right_upper_part + right_lower_part + 1
print(left, right)
# Driver code
if __name__ == "__main__":
row = 4
col = 3
x = 2
y = 2
count_left_right(row, col, x, y)
# This code is contributed by ChitraNayal
C#
// C# implementation of the above approach
using System;
class Program
{
// Function to return the number of cells
// in the left and the right diagonal of
// the matrix for a cell (x, y)
static void count_left_right(int n, int m
, int x, int y)
{
int left = 0, right = 0;
// number of cells in the left diagonal
int left_upper_part = Math.Min(x - 1, y - 1);
int left_lower_part = Math.Min(n - x, m - y);
left = left_upper_part + left_lower_part + 1;
// number of cells in the right diagonal
int right_upper_part = Math.Min(m - y, x - 1);
int right_lower_part = Math.Min(y - 1, n - x);
right = right_upper_part + right_lower_part + 1;
Console.WriteLine(left + " " + right);
}
//Driver code
static void Main()
{
int row = 4;
int col = 3;
int x = 2;
int y = 2;
count_left_right(row, col, x, y);
}
// This code is contributed by ANKITRAI1
}
PHP
Javascript
输出:
3 3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。