📌  相关文章
📜  n个男孩和女孩在圆桌旁的座位安排

📅  最后修改于: 2021-06-25 14:59:59             🧑  作者: Mango

有n个男孩和n个女孩围成一圈坐在圆桌旁。任务是找到让n个男孩和n个女孩交替坐在圆桌旁的方式。给定n <10
例子:

Input: n = 5 
Output: 2880

Input: n = 1
Output: 1

方法:

  1. 首先找到在圆桌会议上安排男孩子的总数。
    在桌子上布置男孩的方式数量= (n-1)!
  2. 在安排了男孩之后,现在安排了女孩。在男生就座之后,他们之间没有空间。因此,有n个职位和n个女孩。因此,女孩坐在男孩之间的安排总数为n!
  3. 因此,方法总数=(男孩的布置数量)*(男孩中女孩坐在那里的方法数量)= (n-1)! *(n!)

下面是上述方法的实现:

C++
// C++ program to find number of ways in which
// n boys and n girls can sit alternatively
// sound a round table.
#include 
using namespace std;
 
#define ll long int
 
int main()
{
 
    // Get n
    ll n = 5;
 
    // find fac1 = (n-1)!
    ll fac1 = 1;
    for (int i = 2; i <= n - 1; i++)
        fac1 = fac1 * i;
 
    // Find fac2 = n!
    ll fac2 = fac1 * n;
 
    // Find total number of ways
    ll totalWays = fac1 * fac2;
 
    // Print the total number of ways
    cout << totalWays << endl;
 
    return 0;
}


Java
// Java program to find number of ways
// in which n boys and n girls can sit
// alternatively sound a round table.
import java .io.*;
 
class GFG
{
public static void main(String[] args)
{
 
    // Get n
    long n = 5;
 
    // find fac1 = (n-1)!
    long fac1 = 1;
    for (int i = 2; i <= n - 1; i++)
        fac1 = fac1 * i;
 
    // Find fac2 = n!
    long fac2 = fac1 * n;
 
    // Find total number of ways
    long totalWays = fac1 * fac2;
 
    // Print the total number of ways
    System.out.println(totalWays);
}
}
 
// This code is contributed
// by anuj_67..


Python3
# Python3 program to find number
# of ways in which n boys and n
# girls can sit alternatively
# sound a round table.
 
# Get n
n = 5
 
# find fac1 = (n-1)!
fac1 = 1
for i in range(2, n):
    fac1 = fac1 * i
 
# Find fac2 = n!
fac2 = fac1 * n
 
# Find total number of ways
totalWays = fac1 * fac2
 
# Print the total number of ways
print(totalWays)
 
# This code is contributed
# by sahilshelangia


C#
// C# program to find number of ways
// in which n boys and n girls can sit
// alternatively sound a round table.
using System;
 
class GFG
{
public static void Main()
{
 
    // Get n
    long n = 5;
 
    // find fac1 = (n-1)!
    long fac1 = 1;
    for (int i = 2; i <= n - 1; i++)
        fac1 = fac1 * i;
 
    // Find fac2 = n!
    long fac2 = fac1 * n;
 
    // Find total number of ways
    long totalWays = fac1 * fac2;
 
    // Print the total number of ways
    Console.WriteLine(totalWays);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
2880