📜  给定范围内的对数,其比率等于其数字乘积的比率

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

给定范围内的对数,其比率等于其数字乘积的比率

给定两个整数LR ,任务是找到[L, R]范围内的无序整数对(A, B)的计数,使得AB的比率与乘积的比率相同A的数字和 B 的数字的乘积

例子:

方法:给定的问题可以使用以下讨论的步骤来解决:

  • 创建一个函数来计算数字的位数的乘积。
  • 使用ab对每一对(a, b)遍历[L, R]范围内的所有无序整数对, a : b 等价于a 的数字乘积: b 的数字乘积当且仅当a * b 的数字乘积 = b * a 的数字乘积
  • 使用上述观察,在变量cntPair中保持(a, b)的有效对的计数,使得a * b 的数字的乘积 = b * a 的数字的乘积
  • 完成上述步骤后,打印cntPair的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the product of
// digits of the given number
int getProduct(int n)
{
    int product = 1;
    while (n != 0) {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
 
// Function to find the count of pairs
// (a, b) such that a:b = (product of
// digits of a):(product of digits of b)
int countPairs(int L, int R)
{
    // Stores the count of the valid pairs
    int cntPair = 0;
 
    // Loop to iterate over all unordered
    // pairs (a, b)
    for (int a = L; a <= R; a++) {
        for (int b = a + 1; b <= R; b++) {
 
            // Stores the product of
            // digits of a
            int x = getProduct(a);
 
            // Stores the product of
            // digits of b
            int y = getProduct(b);
 
            // If x!=0 and y!=0 and a:b
            // is equivalent to x:y
            if (x && y && (a * y) == (b * x)) {
 
                // Increment valid pair count
                cntPair++;
            }
        }
    }
 
    // Return Answer
    return cntPair;
}
 
// Driver code
int main()
{
    int L = 1;
    int R = 100;
 
    // Function Call
    cout << countPairs(1, 100);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the product of
// digits of the given number
public static int getProduct(int n)
{
    int product = 1;
    while (n != 0) {
        product = product * (n % 10);
        n = n / 10;
    }
    return product;
}
 
// Function to find the count of pairs
// (a, b) such that a:b = (product of
// digits of a):(product of digits of b)
public static int countPairs(int L, int R)
{
    // Stores the count of the valid pairs
    int cntPair = 0;
 
    // Loop to iterate over all unordered
    // pairs (a, b)
    for (int a = L; a <= R; a++) {
        for (int b = a + 1; b <= R; b++) {
 
            // Stores the product of
            // digits of a
            int x = getProduct(a);
 
            // Stores the product of
            // digits of b
            int y = getProduct(b);
 
            // If x!=0 and y!=0 and a:b
            // is equivalent to x:y
            if (x !=0  && y != 0 && (a * y) == (b * x)) {
 
                // Increment valid pair count
                cntPair++;
            }
        }
    }
 
    // Return Answer
    return cntPair;
}
 
// Driver code
public static void  main(String args[])
{
    int L = 1;
    int R = 100;
 
    // Function Call
    System.out.println(countPairs(L, R));
}
 
}
 
// This code is contributed by _saurabh_jaiswal.


Python3
# Python 3 program for the above approach
 
# Function to find the product of
# digits of the given number
def getProduct(n):
    product = 1
    while (n != 0):
        product = product * (n % 10)
        n = n // 10
    return product
 
# Function to find the count of pairs
# (a, b) such that a:b = (product of
# digits of a):(product of digits of b)
def countPairs(L, R):
    # Stores the count of the valid pairs
    cntPair = 0
 
    # Loop to iterate over all unordered
    # pairs (a, b)
    for a in range(L,R+1,1):
        for b in range(a + 1,R+1,1):
            # Stores the product of
            # digits of a
            x = getProduct(a)
 
            # Stores the product of
            # digits of b
            y = getProduct(b)
 
            # If x!=0 and y!=0 and a:b
            # is equivalent to x:y
            if (x and y and (a * y) == (b * x)):
                # Increment valid pair count
                cntPair += 1
 
    # Return Answer
    return cntPair
 
# Driver code
if __name__ == '__main__':
    L = 1
    R = 100
 
    # Function Call
    print(countPairs(1, 100))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
public class GFG{
 
    // Function to find the product of
    // digits of the given number
    public static int getProduct(int n)
    {
        int product = 1;
        while (n != 0) {
            product = product * (n % 10);
            n = n / 10;
        }
        return product;
    }
 
    // Function to find the count of pairs
    // (a, b) such that a:b = (product of
    // digits of a):(product of digits of b)
    public static int countPairs(int L, int R)
    {
        // Stores the count of the valid pairs
        int cntPair = 0;
     
        // Loop to iterate over all unordered
        // pairs (a, b)
        for (int a = L; a <= R; a++) {
            for (int b = a + 1; b <= R; b++) {
     
                // Stores the product of
                // digits of a
                int x = getProduct(a);
     
                // Stores the product of
                // digits of b
                int y = getProduct(b);
     
                // If x!=0 and y!=0 and a:b
                // is equivalent to x:y
                if (x !=0  && y != 0 && (a * y) == (b * x)) {
     
                    // Increment valid pair count
                    cntPair++;
                }
            }
        }
     
        // Return Answer
        return cntPair;
    }
 
    // Driver code
    public static void Main(string []args)
    {
        int L = 1;
        int R = 100;
     
        // Function Call
        Console.WriteLine(countPairs(L, R));
    }
 
}
 
// This code is contributed by AnkThon


Javascript


输出:
43

时间复杂度: O(N 2 *log N) 其中N表示给定范围内的整数个数,即 R – L。
辅助空间: O(1)