📜  连续底函数的值:F(x)= F(地板(x / 2))+ x

📅  最后修改于: 2021-04-25 00:04:10             🧑  作者: Mango

给定正整数数组。对于数组的每个元素x,我们需要找到定义为F(x)= F(floor(x / 2))+ x的连续底函数的值,其中F(0)= 0。

例子 :-

Input : arr[] = {6, 8}
Output : 10 15

Explanation : F(6) = 6 + F(3)
                   = 6 + 3 + F(1)
                   = 6 + 3 + 1 + F(0)
                   = 10
Similarly F(8) = 15

基本方法:对于给定的x值,我们可以使用简单的递归函数来计算F(x):

int func(int x)
{
    if (x == 0) 
        return 0;
    return (x + func(floor(x/2)));
}

在这种方法中,如果我们有n个查询,则每个查询元素的取值为O(x)

一种有效的方法是使用记忆。我们构造一个数组,该数组为x的每个可能值保留F(x)的值。如果尚未计算,我们将计算该值。否则,我们返回值。

C++
// C++ program for finding value 
// of continuous floor function
#include 
  
#define max 10000
using namespace std;
  
int dp[max];
  
void initDP()
    {
         for (int i = 0; i < max; i++)
             dp[i] = -1;
    }
  
// function to return value of F(n)
int func(int x)
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
  
        return dp[x];
    }
  
void printFloor(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            cout << func(arr[i]) << " ";
    }
  
// Driver code
int main()
    {
        // call the initDP() to fill DP array
        initDP();
  
        int arr[] = { 8, 6 };
        int n = sizeof(arr) / sizeof(arr[0]);
  
        printFloor(arr, n);
  
        return 0;
    }


Java
// Java program for finding value
// of continuous floor function
class GFG 
{
    static final int max = 10000;
    static int dp[] = new int[max];
      
    static void initDP() 
    {
        for (int i = 0; i < max; i++)
            dp[i] = -1;
    }
      
    // function to return value of F(n)
    static int func(int x) 
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
      
        return dp[x];
    }
      
    static void printFloor(int arr[], int n) 
    {
        for (int i = 0; i < n; i++)
            System.out.print(func(arr[i]) + " ");
    }
      
    // Driver code
    public static void main(String[] args) 
    {
          
        // call the initDP() to fill DP array
        initDP();
      
        int arr[] = {8, 6};
        int n = arr.length;
      
        printFloor(arr, n);
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python3 program for finding value 
# of continuous floor function 
  
max = 10000
  
dp = [0] * max
  
# function to initialize the DP array
def initDP() :
      
        for i in range(max) : 
                dp[i] = -1
      
# function to return value of F(n) 
def func(x) :
  
    if (x == 0) :
        return 0
          
    if (dp[x] == -1) :
        dp[x] = x + func(x // 2)
  
    return dp[x] 
      
def printFloor(arr, n) :
      
    for i in range(n) :
                  
        print(func(arr[i]), end = " ")
  
# Driver Code
if __name__ == "__main__" :
  
        # call the initDP() to 
        # fill DP array         
        initDP()
  
        arr = [8, 6]
        n = len(arr)
  
        printFloor(arr, n)
  
# This code is contributed by Ryuga


C#
// C# program for finding value 
// of continuous floor function
using System;
  
class GFG 
{
    static int max = 10000;
    static int []dp = new int[max];
      
    static void initDP() 
    {
        for (int i = 0; i < max; i++)
            dp[i] = -1;
    }
      
    // function to return value of F(n)
    static int func(int x) 
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
      
        return dp[x];
    }
      
    static void printFloor(int []arr, int n) 
    {
        for (int i = 0; i < n; i++)
            Console.Write(func(arr[i]) + " ");
    }
      
    // Driver code
    public static void Main() 
    {
          
        // call the initDP() to fill DP array
        initDP();
      
        int []arr = {8, 6};
        int n = arr.Length;
      
        printFloor(arr, n);
    }
}
  
// This code is contributed by nitin mittal


PHP


输出:

15 10