📌  相关文章
📜  给定范围内的对数,其乘积和之和等于连接数

📅  最后修改于: 2021-04-23 08:00:00             🧑  作者: Mango

给定两个数字AB ,任务是查找范围[A,B]中的对(X,Y)的数量,以使(X * Y)+(X + Y)等于级联形成的数量XY的
例子:

方法 :
我们可以看到,任何形式的数字[9,99,999,9999,…。]都可以满足所有其他值的条件。

因此,请按照以下步骤解决问题:

  1. 计算范围[A,B]中{9,99,999,9999 ,…。}形式的Y的可能值个数,并将其存储在countY中
  2. 将[A,B]范围内X的可能值的数量计为countX
countX = (B - A + 1)
  1. 所需的计数将是X和Y的可能计数的乘积,即
answer = countX * countY

下面是上述方法的实现:

C++
// C++ program to count
// all the possible pairs
// with X*Y + (X + Y) equal to
// number formed by
// concatenating X and Y
 
#include 
using namespace std;
 
// Function for counting pairs
int countPairs(int A, int B)
{
 
    int countY = 0,
        countX = (B - A) + 1,
        next_val = 9;
 
    // Count possible vlues
    // of Y
    while (next_val <= B) {
        if (next_val >= A) {
            countY += 1;
        }
        next_val = next_val * 10 + 9;
    }
 
    return (countX * countY);
}
 
// Driver Code
int main()
{
    int A = 1;
    int B = 16;
    cout << countPairs(A, B);
    return 0;
}


Java
// Java program to count
// all the possible pairs
// with X*Y + (X + Y) equal to
// number formed by
// concatenating X and Y
import java.util.*;
class GFG{
 
// Function for counting pairs
static int countPairs(int A, int B)
{
    int countY = 0,
        countX = (B - A) + 1,
        next_val = 9;
 
    // Count possible vlues
    // of Y
    while (next_val <= B)
    {
        if (next_val >= A)
        {
            countY += 1;
        }
        next_val = next_val * 10 + 9;
    }
    return (countX * countY);
}
 
// Driver Code
public static void main(String args[])
{
    int A = 1;
    int B = 16;
    System.out.print(countPairs(A, B));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 program to count
# all the possible pairs
# with X*Y + (X + Y) equal to
# number formed by
# concatenating X and Y
 
# Function for counting pairs
def countPairs(A, B):
 
    countY = 0
    countX = (B - A) + 1
    next_val = 9
 
    # Count possible vlues
    # of Y
    while (next_val <= B):
        if (next_val >= A):
            countY += 1
        next_val = next_val * 10 + 9
 
    return (countX * countY)
 
# Driver Code
if __name__ == '__main__':
     
    A = 1
    B = 16
     
    print(countPairs(A, B))
 
# This code is contributed by mohit kumar 29


C#
// C# program to count
// all the possible pairs
// with X*Y + (X + Y) equal to
// number formed by
// concatenating X and Y
using System;
class GFG{
 
// Function for counting pairs
static int countPairs(int A, int B)
{
    int countY = 0,
        countX = (B - A) + 1,
        next_val = 9;
 
    // Count possible vlues
    // of Y
    while (next_val <= B)
    {
        if (next_val >= A)
        {
            countY += 1;
        }
        next_val = next_val * 10 + 9;
    }
    return (countX * countY);
}
 
// Driver Code
public static void Main()
{
    int A = 1;
    int B = 16;
    Console.Write(countPairs(A, B));
}
}
 
// This code is contributed by Akanksha_Rai


Javascript


输出:
16

时间复杂度: O(log 10 (B))
空间复杂度: O(1)