📌  相关文章
📜  在1到N范围内的对(A,B)的计数,以使A的最后一位等于B的第一位

📅  最后修改于: 2021-04-22 07:26:28             🧑  作者: Mango

给定数字N ,任务是找到范围[1,N]中的对数(A,B) ,以使A的最后一位等于B的第一位,而A的第一位等于到B的最后一位。

例子:

方法:对于每对整数(i,j)(0≤i,j≤9) ,让我们定义c i,j (1≤k≤N) ,它是k的第一位数等于i ,最后一位等于j 。通过使用c i,j ,可以通过i = 0 9j = 0 9 c i,j * c j,i来计算问题的答案。

下面是上述方法的实现:

CPP
// C++ program to implement the above approach
  
#include 
using namespace std;
  
// Function to Count of pairs (A, B) in range 1 to N
int pairs(int n)
{
    vector > c(10, vector(10, 0));
  
    int tmp = 1;
  
    // count C i, j
    for (int i = 1; i <= n; i++) {
        if (i >= tmp * 10)
            tmp *= 10;
        c[i / tmp][i % 10]++;
    }
  
    // Calculate number of pairs
    long long ans = 0;
    for (int i = 1; i < 10; i++)
        for (int j = 1; j < 10; j++)
            ans += (long long)c[i][j] * c[j][i];
  
    return ans;
}
  
// Driver code
int main()
{
    int n = 25;
  
    // Function call
    cout << pairs(n);
  
    return 0;
}


Java
// Java program to implement the above approach
  
class GFG{
   
// Function to Count of pairs (A, B) in range 1 to N
static int pairs(int n)
{
    int [][]c = new int[10][10];
   
    int tmp = 1;
   
    // count C i, j
    for (int i = 1; i <= n; i++) {
        if (i >= tmp * 10)
            tmp *= 10;
        c[i / tmp][i % 10]++;
    }
   
    // Calculate number of pairs
    int ans = 0;
    for (int i = 1; i < 10; i++)
        for (int j = 1; j < 10; j++)
            ans += c[i][j] * c[j][i];
   
    return ans;
}
   
// Driver code
public static void main(String[] args)
{
    int n = 25;
   
    // Function call
    System.out.print(pairs(n));
   
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement the above approach
  
# Function to Count of pairs (A, B) in range 1 to N
def pairs(n):
    c = [[0 for i in range(10)] for i in range(10)]
  
    tmp = 1
  
    # count C i, j
    for i in range(1, n + 1):
        if (i >= tmp * 10):
            tmp *= 10
        c[i // tmp][i % 10] += 1
  
    # Calculate number of pairs
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += c[i][j] * c[j][i]
  
    return ans
  
# Driver code
if __name__ == '__main__':
    n = 25
  
    # Function call
    print(pairs(n))
  
# This code is contributed by mohit kumar 29


C#
// C# program to implement the above approach
using System;
  
class GFG{
    
// Function to Count of pairs (A, B) in range 1 to N
static int pairs(int n)
{
    int [,]c = new int[10, 10];
    
    int tmp = 1;
    
    // count C i, j
    for (int i = 1; i <= n; i++) {
        if (i >= tmp * 10)
            tmp *= 10;
        c[i / tmp, i % 10]++;
    }
    
    // Calculate number of pairs
    int ans = 0;
    for (int i = 1; i < 10; i++)
        for (int j = 1; j < 10; j++)
            ans += c[i, j] * c[j, i];
    
    return ans;
}
    
// Driver code
public static void Main(String[] args)
{
    int n = 25;
    
    // Function call
    Console.Write(pairs(n));
    
}
}
  
// This code is contributed by Rajput-Ji


输出:
17

时间复杂度: O(N)