有N个男孩坐在圆桌旁。任务是找到N个男孩可以坐在圆桌旁以使两个特定男孩坐在一起的方式。
例子:
Input: N = 5
Output: 48
2 boy can be arranged in 2! ways and other boys
can be arranged in (5 – 1)! (1 is subtracted because the
previously selected two boys will be considered as a single boy now)
So, total ways are 2! * 4! = 48.
Input: N = 9
Output: 80640
方法:
- 首先,两个可以安排两个男孩!方法。
- 剩余的男孩和前两个男孩对的排列方式为(n – 1)!。
- 因此,总计方式= 2! *(n – 1)! 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the total count of ways
int Total_Ways(int n)
{
// Find (n - 1) factorial
int fac = 1;
for (int i = 2; i <= n - 1; i++) {
fac = fac * i;
}
// Return (n - 1)! * 2!
return (fac * 2);
}
// Driver code
int main()
{
int n = 5;
cout << Total_Ways(n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
// Function to return the total count of ways
static int Total_Ways(int n)
{
// Find (n - 1) factorial
int fac = 1;
for (int i = 2; i <= n - 1; i++)
{
fac = fac * i;
}
// Return (n - 1)! * 2!
return (fac * 2);
}
// Driver code
public static void main (String[] args)
{
int n = 5;
System.out.println (Total_Ways(n));
}
}
// This code is contributed by Tushil.
Python3
# Python3 implementation of the approach
# Function to return the total count of ways
def Total_Ways(n) :
# Find (n - 1) factorial
fac = 1;
for i in range(2, n) :
fac = fac * i;
# Return (n - 1)! * 2!
return (fac * 2);
# Driver code
if __name__ == "__main__" :
n = 5;
print(Total_Ways(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the total count of ways
static int Total_Ways(int n)
{
// Find (n - 1) factorial
int fac = 1;
for (int i = 2; i <= n - 1; i++)
{
fac = fac * i;
}
// Return (n - 1)! * 2!
return (fac * 2);
}
// Driver code
static public void Main ()
{
int n = 5;
Console.Write(Total_Ways(n));
}
}
// This code is contributed by ajit..
Javascript
输出:
48