📌  相关文章
📜  在给定规则生成的矩阵中查找元素

📅  最后修改于: 2021-04-24 17:23:10             🧑  作者: Mango

鉴于一些规则,以生成一个N * N的矩阵垫[] []和两个整数RC中,任务是找到在r行和C的元素。规则如下:

  1. 第一行是从1开始且d = 1 (共同差)的AP系列。
  2. 对于(i,j)处所有> i> j的元素,它们的值为0
  3. 其余元素紧随其后, Element(i,j)= Element(i – 1,j)+ Element(i – 1,j – 1)

例子:

方法:

  • 基本的关键是观察模式,首先观察的是,每一行在(i,i)的元素之后都会有一个AP。行号j的共同区别是pow(2,j – 1)
  • 现在,我们需要找到每一行的第一项以找到任何元素(R,C) 。如果仅考虑每行中的起始元素(即对角线元素),则对于从2N的每一行R ,我们都可以观察到它等于(R +1)* pow(2,R – 2)
  • 这样,如果R> C,则元素为0否则C – R是在AP所需元件的位置,其存在于r行一个我们已经知道起始术语和共用差。因此,我们可以找到元素为start + d *(C – R)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the element in the rth row
// and cth column from the required matrix
int getElement(int N, int r, int c)
{
  
    // Condition for lower half of matrix
    if (r > c)
        return 0;
  
    // Condition if element is in first row
    if (r == 1) {
        return c;
    }
  
    // Starting element of AP in row r
    int a = (r + 1) * pow(2, r - 2);
  
    // Common difference of AP in row r
    int d = pow(2, r - 1);
  
    // Position of element to find
    // in AP in row r
    c = c - r;
  
    int element = a + d * c;
  
    return element;
}
  
// Driver Code
int main()
{
    int N = 4, R = 3, C = 4;
  
    cout << getElement(N, R, C);
  
    return 0;
}


Java
// Java implementation of the above approach 
import java.io.*; 
import java.util.*;
  
class GFG 
{ 
      
// Function to return the element 
// in the rth row and cth column
// from the required matrix 
static int getElement(int N, int r, int c) 
{ 
  
    // Condition for lower half of matrix 
    if (r > c) 
        return 0; 
  
    // Condition if element is in first row 
    if (r == 1) 
    { 
        return c; 
    } 
  
    // Starting element of AP in row r 
    int a = (r + 1) * (int)(Math.pow(2,(r - 2))); 
  
    // Common difference of AP in row r 
    int d = (int)(Math.pow(2,(r - 1))); 
  
    // Position of element to find 
    // in AP in row r 
    c = c - r; 
  
    int element = a + d * c; 
  
    return element; 
} 
  
// Driver Code 
public static void main(String[] args) 
{ 
    int N = 4, R = 3, C = 4; 
  
    System.out.println(getElement(N, R, C)); 
} 
} 
  
// This code is contributed by Krikti


Python3
# Python3 implementation of the approach 
  
# Function to return the element in the rth row 
# and cth column from the required matrix 
def getElement(N, r, c) : 
  
    # Condition for lower half of matrix 
    if (r > c) :
        return 0; 
  
    # Condition if element is in first row 
    if (r == 1) :
        return c; 
      
  
    # Starting element of AP in row r 
    a = (r + 1) * pow(2, r - 2); 
  
    # Common difference of AP in row r 
    d = pow(2, r - 1); 
  
    # Position of element to find 
    # in AP in row r 
    c = c - r; 
  
    element = a + d * c; 
  
    return element; 
  
  
# Driver Code 
if __name__ == "__main__" : 
  
    N = 4; R = 3; C = 4; 
  
    print(getElement(N, R, C)); 
      
# This Code is contributed by AnkitRai01


C#
// C# implementation of the above approach 
using System;
      
class GFG 
{ 
      
// Function to return the element 
// in the rth row and cth column
// from the required matrix 
static int getElement(int N, int r, int c) 
{ 
  
    // Condition for lower half of matrix 
    if (r > c) 
        return 0; 
  
    // Condition if element is in first row 
    if (r == 1) 
    { 
        return c; 
    } 
  
    // Starting element of AP in row r 
    int a = (r + 1) * (int)(Math.Pow(2,(r - 2))); 
  
    // Common difference of AP in row r 
    int d = (int)(Math.Pow(2,(r - 1))); 
  
    // Position of element to find 
    // in AP in row r 
    c = c - r; 
  
    int element = a + d * c; 
  
    return element; 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
    int N = 4, R = 3, C = 4; 
  
    Console.WriteLine(getElement(N, R, C)); 
} 
}
  
// This code is contributed by Princi Singh


输出:
12