📌  相关文章
📜  查找表中包含X的单元格数

📅  最后修改于: 2021-04-27 17:37:29             🧑  作者: Mango

给定两个整数NX。 N代表表的行数和列数。并且表中的第i行和第j列的元素是i * j 。任务是查找表中包含X的单元格数。

例子:

方法:
很容易看到数字x只能连续出现一次。如果x在第i行中包含,则列号将为x / i 。如果x可被i整除,则x包含在第i行中。让我们检查x是否除以ix / i <= n 。如果满足这些条件,请更新答案。

下面是上述方法的实现:

C++
// CPP program to find number of
// cells in the table contains X
#include 
using namespace std;
 
// Function to find number of
// cells in the table contains X
int Cells(int n, int x)
{
    int ans = 0;
    for (int i = 1; i <= n; i++)
        if (x % i == 0 && x / i <= n)
            ans++;
 
    return ans;
}
 
// Driver code
int main()
{
    int n = 6, x = 12;
 
    // Function call
    cout << Cells(n, x);
 
    return 0;
}


Java
// Java program to find number of
// cells in the table contains X
class GFG
{
 
    // Function to find number of
    // cells in the table contains X
    public static int Cells(int n, int x)
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
            if (x % i == 0 && x / i <= n)
                ans++;
 
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 6, x = 12;
 
        // Function call
        System.out.println(Cells(n, x));
    }
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 program to find number of
# cells in the table contains X
 
# Function to find number of
# cells in the table contains X
def Cells(n, x):
 
    ans = 0;
    for i in range(1, n + 1):
        if (x % i == 0 and x / i <= n):
            ans += 1;
 
    return ans;
 
# Driver code
if __name__ == '__main__':
 
    n = 6; x = 12;
 
    # Function call
    print(Cells(n, x));
 
# This code is contributed by 29AjayKumar


C#
// C# program to find number of
// cells in the table contains X
using System;
 
class GFG
{
    // Function to find number of
    // cells in the table contains X
    static int Cells(int n, int x)
    {
        int ans = 0;
        for (int i = 1; i <= n; i++)
            if (x % i == 0 && x / i <= n)
                ans++;
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 6, x = 12;
         
        // Function call
        Console.WriteLine(Cells(n,x));
    }
}
 
// This code is contributed by nidhiva


Java
// Java program to find number of
// cells in the table contains X
class GFG {
 
    // Function to find number of
    // cells in the table contains X
    public static int Cells(int n, int x)
    {
        if (n <= 0 || x <= 0 || x > n * n)
            return 0;
        int i = 0, count = 0;
        while (++i * i < x)
            if (x % i == 0 && x <= n * i)
                count += 2;
        return i * i == x ? count + 1 : count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 6, x = 12;
 
        // Function call
        System.out.println(Cells(n, x));
    }
}
 
// This code is contributed by stephenbrasel


输出
4

方法

忽略带有负平方的情况。如果n为0,则正方形中将没有任何数字;如果x为0,则它将不会出现在正方形中,因此在两种情况下均返回0。如果x大于n ^ 2,则它将不在平方中,因此在这种情况下也应返回0。
接下来,遍历从1到x的平方根的所有数字i ,如果ix的因数,并且x / i <= n ,则由于n的存在, x在n平方图中至少有两个其他位置。关联属性: i *(x / i)(x / i)* i ,例如,如果x为12并且i为3:3 * 4和4 * 3。
最后,找出x的平方根是否为整数。如果是,它将在n平方表的对角线上再出现一次,即所有平方的列表。
| 1 | 2 | 3 | 4 | 5 | 6 |

| 2 | 4 | 6 | 8 | 10 | 12 |

| 3 | 6 | 9 | 12 | 15 | 18 |

| 4 | 8 | 12 | 16 | 20 | 24 |

| 5 | 10 | 15 | 20 | 25 | 30 |

| 6 | 12 | 18 | 24 | 30 | 36 |

下面是上述方法的实现:

Java

// Java program to find number of
// cells in the table contains X
class GFG {
 
    // Function to find number of
    // cells in the table contains X
    public static int Cells(int n, int x)
    {
        if (n <= 0 || x <= 0 || x > n * n)
            return 0;
        int i = 0, count = 0;
        while (++i * i < x)
            if (x % i == 0 && x <= n * i)
                count += 2;
        return i * i == x ? count + 1 : count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 6, x = 12;
 
        // Function call
        System.out.println(Cells(n, x));
    }
}
 
// This code is contributed by stephenbrasel
输出
4