📌  相关文章
📜  一个数字的可能排列计数,仅表示为2、4和6的总和

📅  最后修改于: 2021-04-26 08:59:06             🧑  作者: Mango

给定整数N ,任务是找出排列数量,其中N只能表示为2 s, 4 s和6 s之和。

注意:同一组合的不同排列也将计算在内。

例子:

方法:为了解决此问题,我们使用动态编程方法:

  • 由于可以增加到N的可能数字是2、4和6,因此可以将它们视为基本情况。通过使用基本案例,可以计算更多案例。
  • 让我们考虑一个大小为N + 1dp []数组,该数组将仅使用2、4、6来计算和存储在每个索引i处形成值i的可能方法。
  • 因此,dp [N]包含所需的答案。

下面是上述方法的实现:

C++
// C++ code for above implementation
  
#include 
using namespace std;
  
// Returns number of ways
// to reach score n
int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
  
    int table[n + 1], i;
  
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
  
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
  
    for (i = 8; i <= n; i = i + 2) {
  
        table[i] = table[i - 2]
                   + table[i - 4]
                   + table[i - 6];
    }
  
    return table[n];
}
  
// Driver Code
int main(void)
{
    int n = 8;
    cout << count(n);
  
    return 0;
}


Java
// Java code for above implementation
import java.util.*;
class GFG{
  
// Returns number of ways
// to reach score n
static int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
  
    int table[] = new int[n + 1];
    int i;
  
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
  
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
  
    for (i = 8; i <= n; i = i + 2) 
    {
        table[i] = table[i - 2] + 
                   table[i - 4] + 
                   table[i - 6];
    }
  
    return table[n];
}
  
// Driver Code
public static void main(String args[])
{
    int n = 8;
    System.out.print(count(n));
}
}
  
// This code is contributed by Akanksha_Rai


Python3
# Python3 code for above implementation 
  
# Returns number of ways 
# to reach score n 
def count(n) :
  
    # table[i] will store count 
    # of solutions for value i. 
    if (n == 2) :
        return 1; 
    elif (n == 4) :
        return 2; 
    elif (n == 6) :
        return 4; 
  
    table = [0] * (n + 1); 
  
    # Initialize all table 
    # values as 0 
    for i in range(n + 1) :
        table[i] = 0; 
  
    # Base case (If given value 
    # is 0, 1, 2, or 4) 
    table[0] = 0; 
    table[2] = 1; 
    table[4] = 2; 
    table[6] = 4; 
  
    for i in range(8 , n + 1, 2) :
  
        table[i] = table[i - 2] + \
                   table[i - 4] + \
                   table[i - 6]; 
  
    return table[n]; 
  
# Driver Code 
if __name__ == "__main__" : 
  
    n = 8; 
    print(count(n)); 
  
# This code is contributed by AnkitRai01


C#
// C# code for above implementation
using System;
class GFG{
  
// Returns number of ways
// to reach score n
static int count(int n)
{
    // table[i] will store count
    // of solutions for value i.
    if (n == 2)
        return 1;
    else if (n == 4)
        return 2;
    else if (n == 6)
        return 4;
  
    int []table = new int[n + 1];
    int i;
  
    // Initialize all table
    // values as 0
    for (i = 0; i < n + 1; i++)
        table[i] = 0;
  
    // Base case (If given value
    // is 0, 1, 2, or 4)
    table[0] = 0;
    table[2] = 1;
    table[4] = 2;
    table[6] = 4;
  
    for (i = 8; i <= n; i = i + 2) 
    {
        table[i] = table[i - 2] + 
                   table[i - 4] + 
                   table[i - 6];
    }
  
    return table[n];
}
  
// Driver Code
public static void Main()
{
    int n = 8;
    Console.Write(count(n));
}
}
  
// This code is contributed by Code_Mech


输出:
7

时间复杂度: O(N)
辅助空间复杂度: O(N)