📜  电话号码

📅  最后修改于: 2021-06-25 16:42:30             🧑  作者: Mango

在数学中,电话号码对合数字是一个整数序列,该整数对具有n个用户的电话系统中的连接模式数进行计数,其中在成对的用户之间建立连接。这些数字还描述了n个顶点的完整图的匹配数,n个元素上的对合的置换数,hermite多项式系数的绝对值之和,n个像元的标准Young tableaux数以及对称组不可约表示的度数之和。
的电话号码也被用来计数的方法来代替乌鸦的数量上的n×n个棋盘以这样的方式使得没有两个乌鸦相互攻击和以这样的方式的乌鸦的结构是电路板的对角线反射下对称。
可以通过以下重复关系来评估电话号码:

给定正整数n 。任务是找到第n个电话号码。
例子 :

Input : n = 4
Output : 10

Input : n = 6
Output : 76

下面是基于上述递归公式查找第n个电话号码的简单实现。

C++
// CPP Program to find the nth telephone number.
#include 
using namespace std;
 
// return the nth telephone number
int telephonenumber(int n)
{
    // base step
    if (n == 0 || n == 1)
        return 1;
 
    // recursive step
    return telephonenumber(n - 1) +
          (n - 1) * telephonenumber(n - 2);
}
 
// Driven Program
int main()
{
    int n = 6;
    cout << telephonenumber(n) << endl;
    return 0;
}


Java
// JAVA Code to find the nth
// telephone number.
import java.util.*;
 
class GFG {
     
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        // base step
        if (n == 0 || n == 1)
            return 1;
      
        // recursive step
        return telephonenumber(n - 1) +
              (n - 1) * telephonenumber(n - 2);
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 6;
        System.out.println(telephonenumber(n));
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to find the
# nth telephone number.
 
# return the nth telephone number
def telephonenumber (n):
 
    # base step
    if n == 0 or n == 1:
        return 1
         
    # recursive step
    return (telephonenumber(n - 1) + (n - 1)
            * telephonenumber(n - 2))
 
# Driven Program
n = 6
print(telephonenumber(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Code to find the nth
// telephone number.
using System;
 
class GFG {
 
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        // base step
        if (n == 0 || n == 1)
            return 1;
 
        // recursive step
        return telephonenumber(n - 1) +
            (n - 1) * telephonenumber(n - 2);
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 6;
         
        Console.Write(telephonenumber(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program to find the nth telephone number.
#include 
using namespace std;
 
// return the nth telephone number
int telephonenumber(int n)
{
    int dp[n + 1];
    memset(dp, 0, sizeof(dp));
 
    // Base case
    dp[0] = dp[1] = 1;
 
    // finding ith telephone number, where 2 <= i <= n.
    for (int i = 2; i <= n; i++)
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
 
    return dp[n];
}
 
// Driver Program
int main()
{
    int n = 6;
    cout << telephonenumber(n) << endl;
    return 0;
}


Java
// JAVA Code to find nth Telephone Number
import java.util.*;
 
class GFG {
     
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        int dp[] = new int[n + 1];
        
        // Base case
        dp[0] = dp[1] = 1;
      
        // finding ith telephone number,
        // where 2 <= i <= n.
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
      
        return dp[n];
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
         int n = 6;
         System.out.println(telephonenumber(n));
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to find the
# nth telephone number.
 
# return the nth telephone number
def telephonenumber (n):
    dp = [0] * (n + 1)
     
    # Base case
    dp[0] = dp[1] = 1
     
    # finding ith telephone number,
    # where 2 <= i <= n.
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]
         
    return dp[n]
     
# Driver Code
n = 6
print(telephonenumber(n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Code to find nth Telephone Number
using System;
 
class GFG {
 
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        int[] dp = new int[n + 1];
 
        // Base case
        dp[0] = dp[1] = 1;
 
        // finding ith telephone number,
        // where 2 <= i <= n.
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
 
        return dp[n];
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 6;
         
        Console.Write(telephonenumber(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

76

以下是使用动态编程查找第n个电话号码的有效实现:

C++

// CPP Program to find the nth telephone number.
#include 
using namespace std;
 
// return the nth telephone number
int telephonenumber(int n)
{
    int dp[n + 1];
    memset(dp, 0, sizeof(dp));
 
    // Base case
    dp[0] = dp[1] = 1;
 
    // finding ith telephone number, where 2 <= i <= n.
    for (int i = 2; i <= n; i++)
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
 
    return dp[n];
}
 
// Driver Program
int main()
{
    int n = 6;
    cout << telephonenumber(n) << endl;
    return 0;
}

Java

// JAVA Code to find nth Telephone Number
import java.util.*;
 
class GFG {
     
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        int dp[] = new int[n + 1];
        
        // Base case
        dp[0] = dp[1] = 1;
      
        // finding ith telephone number,
        // where 2 <= i <= n.
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
      
        return dp[n];
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
         int n = 6;
         System.out.println(telephonenumber(n));
    }
}
 
// This code is contributed by Arnav Kr. Mandal.

Python3

# Python3 code to find the
# nth telephone number.
 
# return the nth telephone number
def telephonenumber (n):
    dp = [0] * (n + 1)
     
    # Base case
    dp[0] = dp[1] = 1
     
    # finding ith telephone number,
    # where 2 <= i <= n.
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]
         
    return dp[n]
     
# Driver Code
n = 6
print(telephonenumber(n))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Code to find nth Telephone Number
using System;
 
class GFG {
 
    // return the nth telephone number
    static int telephonenumber(int n)
    {
        int[] dp = new int[n + 1];
 
        // Base case
        dp[0] = dp[1] = 1;
 
        // finding ith telephone number,
        // where 2 <= i <= n.
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
 
        return dp[n];
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 6;
         
        Console.Write(telephonenumber(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

76