📌  相关文章
📜  在特殊矩阵中计数等于 x 的条目

📅  最后修改于: 2022-05-13 01:57:24.756000             🧑  作者: Mango

在特殊矩阵中计数等于 x 的条目

给定一个 n 阶方阵 (matrix[][]),其中 matrix[i][j] = i*j。找出条目等于给定数字 x 的单元格的数量。
注意:矩阵的索引从 1 开始,即 1<= i,j <= n。
例子 :

Input : matrix[][] = {1, 2, 3, 4,
                      2, 4, 6, 8,
                      3, 6, 9, 12,
                      4, 8, 12, 16}  
                x = 4
Output : 3

Input : matrix[][] = {1, 2, 3, 4,
                      2, 4, 6, 8,
                      3, 6, 9, 12,
                      4, 8, 12, 16}   
                 x = 12
Output : 2

一个简单的方法是遍历整个矩阵并检查单元格值是否等于给定的x,然后相应地增加计数值。这种方法的时间复杂度非常高,等于 O(n^2)。

// traverse and check whole matrix
for (int i=1; i<=n ; i++)
{
    for (int j=1; j<=n; j++)
        if (i * j == x)
            count++;
}
// return count 
return count;

一种有效的方法是仅找到给定 x 在 0 到 x 范围内的因子数,并且这些因子(包括除数和商)必须小于或等于 n(矩阵的阶数)。在这种情况下,时间复杂度将为 O(n)。

// traverse and find the factors
for (int i=1; i<=n && i<=x ; i++)
{
    // x%i == 0 means i is factor of x
    // x/i <= n means i and j are <= n (for i*j=x)
    if ( x/i <= n && x%i ==0)
        count++;
}
// return count 
return count;

C++
// CPP program for counting number of cell
// equals to given x
#include
using namespace std;
 
// function to count factors as number of cell
int count (int n, int x)
{
    int count == 0;
    // traverse and find the factors
    for (int i=1; i<=n && i<=x ; i++)
    {
        // x%i == 0 means i is factor of x
        // x/i <= n means i and j are <= n (for i*j=x)
        if ( x/i <= n && x%i ==0)
            count++;
    }
    // return count
    return count;
}
 
// driver program
int main()
{
    int n = 8;
    // we can manually assume matrix of order 8*8
    // where mat[i][j] = i*j , 0


Java
// Java program for counting number of
// cell equals to given x
class GFG
{
    // function to count factors as
    // number of cell
    static int count (int n, int x)
    {
        int count = 0;
     
        // traverse and find the factors
        for (int i = 1; i <= n && i <= x ;
                                    i++)
        {
            // x%i == 0 means i is factor
            // of x. x/i <= n means i and
            // j are <= n (for i*j=x)
            if ( x / i <= n && x % i == 0)
                count++;
        }
         
        // return count
        return count;
    }
 
    // driver program
    public static void main(String args[])
    {
        int n = 8;
         
        // we can manually assume matrix
        // of order 8*8 where
        // mat[i][j] = i*j , 0


Python3
# Python 3 program for counting
# number of cell equals to given x
 
# function to count factors
# as number of cell
def count(n, x):
    cnt = 0
 
    # traverse and find the factors
    for i in range(1, n + 1):
 
        # // x%i == 0 means i is factor of x
        # x/i <= n means i and j are <= n (for i*j=x)
        if i <= x:
            if x // i <= n and x % i == 0:
                cnt += 1
    return cnt
 
# Driver code
n = 8
x = 24
print(count(n, x))
 
# This code is contributed by Shrikant13


C#
// C# program for counting number
// of cell equals to given x
using System;
 
class GFG {
     
    // function to count factors as
    // number of cell
    static int count (int n, int x) {
         
        int count = 0;
     
        // traverse and find the factors
        for (int i = 1; i <= n &&
             i <= x ; i++)
        {
             
            // x%i == 0 means i is factor
            // of x. x/i <= n means i and
            // j are <= n (for i*j=x)
            if ( x / i <= n && x % i == 0)
                count++;
        }
         
        // return count
        return count;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 8;
         
        // we can manually assume matrix
        // of order 8*8 where
        // mat[i][j] = i*j , 0


PHP


Javascript


输出 :

4