📜  计算将n表示为奇数和的方法

📅  最后修改于: 2021-04-23 16:17:09             🧑  作者: Mango

给定正整数n。计算将“ n”表示为奇数正整数之和的总数。

Input: 4
Output: 3

Explanation
There are only three ways to write 4
as sum of odd integers:
1. 1 + 3
2. 3 + 1
3. 1 + 1 + 1 + 1

Input: 5
Output: 5

简单的方法是找到问题的递归性质。数字“ n”可以写为(n-1)或第(n-2)数字中的奇数整数之和。令写“ n”的方式总数为ways(n)。 ‘ways(n)’的值可以通过递归公式编写,如下所示:

ways(n) = ways(n-1) + ways(n-2)

上面的表达式实际上是斐波那契数的表达式。因此,减少了找到第n斐波那契数的问题。

ways(1) = fib(1) = 1
ways(2) = fib(2) = 1
ways(3) = fib(2) = 2
ways(4) = fib(4) = 3
C++
// C++ program to count ways to write
// number as sum of odd integers
#include
using namespace std;
  
// Function to calculate n'th Fibonacci number
int fib(int n)
{
  /* Declare an array to store Fibonacci numbers. */
  int f[n+1];
  int i;
  
  /* 0th and 1st number of the series are 0 and 1*/
  f[0] = 0;
  f[1] = 1;
  
  for (i = 2; i <= n; i++)
  {
      /* Add the previous 2 numbers in the series
         and store it */
      f[i] = f[i-1] + f[i-2];
  }
  
  return f[n];
}
  
// Return number of ways to write 'n'
// as sum of odd integers
int countOddWays(int n)
{
    return fib(n);
}
  
// Driver code
int main()
{
    int n = 4;
    cout << countOddWays(n) << "\n";
  
    n = 5;
    cout << countOddWays(n);
   return 0;
}


Java
// Java program to count ways to write
// number as sum of odd integers
import java.util.*;
  
class GFG {
      
// Function to calculate n'th Fibonacci number
static int fib(int n) {
      
    /* Declare an array to store Fibonacci numbers. */
    int f[] = new int[n + 1];
    int i;
  
    /* 0th and 1st number of the series are 0 and 1*/
    f[0] = 0;
    f[1] = 1;
  
    for (i = 2; i <= n; i++) {
          
    /* Add the previous 2 numbers in the series
        and store it */
    f[i] = f[i - 1] + f[i - 2];
    }
  
    return f[n];
}
  
// Return number of ways to write 'n'
// as sum of odd integers
static int countOddWays(int n)
{
    return fib(n);
}
  
// Driver code
public static void main(String[] args) {
      
    int n = 4;
    System.out.print(countOddWays(n) + "\n");
  
    n = 5;
    System.out.print(countOddWays(n));
}
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python code to count ways to write
# number as sum of odd integers
  
# Function to calculate n'th 
# Fibonacci number
def fib( n ):
  
    # Declare a list to store 
    # Fibonacci numbers.
    f=list()
      
    # 0th and 1st number of the 
    # series are 0 and 1
    f.append(0)
    f.append(1)
      
    i = 2
    while i


C#
// C# program to count ways to write
// number as sum of odd integers
using System;
  
class GFG {
      
    // Function to calculate n'th
    // Fibonacci number
    static int fib(int n) {
          
        /* Declare an array to store
        Fibonacci numbers. */
        int []f = new int[n + 1];
        int i;
      
        /* 0th and 1st number of the
        series are 0 and 1*/
        f[0] = 0;
        f[1] = 1;
      
        for (i = 2; i <= n; i++)
        {
              
            /* Add the previous 2 numbers
            in the series and store it */
            f[i] = f[i - 1] + f[i - 2];
        }
      
        return f[n];
    }
      
    // Return number of ways to write 'n'
    // as sum of odd integers
    static int countOddWays(int n)
    {
        return fib(n);
    }
      
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(countOddWays(n));
      
        n = 5;
        Console.WriteLine(countOddWays(n));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

3
5

注意:以上实现的时间复杂度为O(n)。可以使用矩阵指数的Fibonacci函数优化进一步优化到O(Logn)时间