📌  相关文章
📜  使用给定的组合计数字符串(由R,G和B组成)的数量

📅  最后修改于: 2021-06-25 10:49:22             🧑  作者: Mango

我们需要制作一个大小为n的字符串。字符串的每个字符都是“ R”,“ B”或“ G”。在最后一个字符串中,至少需要有r个数字“ R”,至少b个数字“ B”和至少g个数字“ G”(这样r + g + b <= n)。我们需要找到许多这样的字符串。
例子:

Input : n = 4, r = 1, 
        b = 1, g = 1.
Output: 36 
No. of 'R' >= 1, 
No. of ‘G’ >= 1, 
No. of ‘B’ >= 1 and 
(No. of ‘R’) + (No. of ‘B’) + (No. of ‘G’) = n
then following cases are possible:
1. RBGR and its 12 permutation
2. RBGB and its 12 permutation
3. RBGG and its 12 permutation
Hence answer is 36.

在询问:Directi

  1. 由于R,B和G必须包含至少给定编号。的时间。剩余值= n-(r + b + g)。
  2. 对剩余的值进行所有组合。
  3. 将每个元素一一考虑为剩余值,然后将所有位移求和。
  4. 退货总数所有组合的排列。
C++
// C++ program to count number of possible strings
// with n characters.
#include
using namespace std;
 
// Function to calculate number of strings
int possibleStrings( int n, int r, int b, int g)
{
    // Store factorial of numbers up to n
    // for further computation
    int fact[n+1];
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i-1] * i;
 
    // Find the remaining values to be added
    int left = n - (r+g+b);
    int sum = 0;
 
    // Make all possible combinations
    // of R, B and G for the remaining value
    for (int i = 0; i <= left; i++)
    {
        for (int j = 0; j<= left-i; j++)
        {
            int k = left - (i+j);
 
            // Compute permutation of each combination
            // one by one and add them.
            sum = sum + fact[n] /
                       (fact[i+r]*fact[j+b]*fact[k+g]);
        }
    }
 
    // Return total no. of strings/permutation
    return sum;
}
 
// Drivers code
int main()
{
    int n = 4, r = 2;
    int b = 0, g = 1;
    cout << possibleStrings(n, r, b, g);
    return 0;
}


Java
// Java program to count number of possible
// strings with n characters.
 
class GFG{
     
    //Function to calculate number of strings
    static int possibleStrings( int n, int r, int b, int g)
    {
     // Store factorial of numbers up to n
     // for further computation
     int fact[] = new int[n+1];
     fact[0] = 1;
     for (int i = 1; i <= n; i++)
         fact[i] = fact[i-1] * i;
 
     // Find the remaining values to be added
     int left = n - (r+g+b);
     int sum = 0;
 
     // Make all possible combinations
     // of R, B and G for the remaining value
     for (int i = 0; i <= left; i++)
     {
         for (int j = 0; j<= left-i; j++)
         {
             int k = left - (i+j);
 
             // Compute permutation of each combination
             // one by one and add them.
             sum = sum + fact[n] /
                        (fact[i+r]*fact[j+b]*fact[k+g]);
         }
     }
 
     // Return total no. of strings/permutation
     return sum;
    }
 
    //Drivers code
    public static void main(String []args)
    {
        int n = 4, r = 2;
         int b = 0, g = 1;
         System.out.println(possibleStrings(n, r, b, g));
    }
}


Python3
# Python 3 program to count number of
# possible strings with n characters.
 
# Function to calculate number of strings
def possibleStrings(n, r, b, g):
     
    # Store factorial of numbers up to n
    # for further computation
    fact = [0 for i in range(n + 1)]
    fact[0] = 1
    for i in range(1, n + 1, 1):
        fact[i] = fact[i - 1] * i
 
    # Find the remaining values to be added
    left = n - (r + g + b)
    sum = 0
 
    # Make all possible combinations of
    # R, B and G for the remaining value
    for i in range(0, left + 1, 1):
        for j in range(0, left - i + 1, 1):
            k = left - (i + j)
 
            # Compute permutation of each
            # combination one by one and add them.
            sum = (sum + fact[n] / (fact[i + r] *
                         fact[j + b] * fact[k + g]))
     
    # Return total no. of
    # strings/permutation
    return sum
 
# Driver code
if __name__ == '__main__':
    n = 4
    r = 2
    b = 0
    g = 1
    print(int(possibleStrings(n, r, b, g)))
     
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to count number of possible
// strings with n characters.
using System;
 
class GFG
{
 
    //Function to calculate number of strings
    static int possibleStrings( int n, int r,
                                int b, int g)
    {
        // Store factorial of numbers up to n
        // for further computation
        int[] fact = new int[n + 1];
        fact[0] = 1;
     
        for (int i = 1; i <= n; i++)
            fact[i] = fact[i - 1] * i;
 
        // Find the remaining values to be added
        int left = n - (r + g + b);
        int sum = 0;
 
        // Make all possible combinations
        // of R, B and G for the remaining value
        for (int i = 0; i <= left; i++)
        {
            for (int j = 0; j <= left - i; j++)
            {
                int k = left - (i + j);
 
                // Compute permutation of each combination
                // one by one and add them.
                sum = sum + fact[n] / (fact[i + r] *
                        fact[j + b] * fact[k + g]);
            }
        }
 
        // Return total no. of strings/permutation
        return sum;
    }
 
    //Drivers code
    public static void Main()
    {
        int n = 4, r = 2;
        int b = 0, g = 1;
        Console.WriteLine(possibleStrings(n, r, b, g));
    }
}
 
// This Code is contributed by Code_Mech.


PHP


Javascript


输出:

22