📜  N长金条支付薪水所需的最低限度裁员数

📅  最后修改于: 2021-04-26 04:54:15             🧑  作者: Mango

鉴于长度为N n等于标记金条,任务是找到需要支付的薪水在N天内削减的最小数量,使得在任何i天工人有金条的的部分。

例子:

方法:
众所周知,任何数字都可以用2的幂的形式表示。因此,如果我们将Gold Bar的长度切成最接近的整数以记录2 (N),那么我们可以表示最大为N的任何数字。 。
例如:当N = 15时,我们可以将数字分为1、2、4、8部分,并使用这些数字可以表示1到15之间的任何数字,如下所示:

For 1 - 1
For 2 - 2
For 3 - 2 + 1
For 4 - 4
For 5 - 4 + 1
For 6 - 4 + 2
For 7 - 4 + 2 + 1
For 8 - 8
For 9 - 8 + 1
For 10 - 8 + 2
For 11 - 8 + 2 + 1
For 12 - 8 + 4
For 13 - 8 + 4 + 1
For 14 - 8 + 4 + 2
For 15 - 8 + 4 + 2 + 1

下面是上述方法的实现。

C++
// CPP Implementation to find
// the minimum number of cuts to
// pay the worker.
#include 
  
using namespace std;
  
// Function to find the minimum
// number of cuts to pay the worker.
int pay(int n)
{
  
    // Nearest Integer to the Log
    // value of the number N
    int cuts = int(log(n)/log(2));
  
    return cuts;
}
  
// Driver code
int main()
{
    int n = 5;
    int cuts = pay(n);
    cout << cuts << endl;
      
    // Cuts Required in the
    // Length of 15
    n = 15;
    cuts = pay(n);
    cout<<(cuts);
      
    return 0;
}
  
// This code is contributed by mohit kumar 29


Java
// JAVA Implementation to find
// the minimum number of cuts to
// pay the worker.
class GFG
{
  
// Function to find the minimum
// number of cuts to pay the worker.
static int pay(int n)
{
  
    // Nearest Integer to the Log
    // value of the number N
    int cuts = (int) (Math.log(n)/Math.log(2));
  
    return cuts;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 5;
    int cuts = pay(n);
    System.out.print(cuts +"\n");
      
    // Cuts Required in the
    // Length of 15
    n = 15;
    cuts = pay(n);
    System.out.print(cuts);
}
}
  
// This code is contributed by 29AjayKumar


Python
# Python Implementation to find 
# the minimum number of cuts to 
# pay the worker.
  
import math
  
# Function to find the minimum 
# number of cuts to pay the worker.
def pay(n):
      
    # Nearest Integer to the Log 
    # value of the number N
    cuts = int(math.log(n, 2))
      
    return cuts
      
# Driver Code
if __name__ == "__main__":
    n = 5
    cuts = pay(n)
    print(cuts)
      
    # Cuts Required in the
    # Length of 15
    n = 15
    cuts = pay(n)
    print(cuts)


C#
// C# Implementation to find
// the minimum number of cuts to
// pay the worker.
using System;
  
class GFG
{
  
// Function to find the minimum
// number of cuts to pay the worker.
static int pay(int n)
{
  
    // Nearest int to the Log
    // value of the number N
    int cuts = (int) (Math.Log(n)/Math.Log(2));
  
    return cuts;
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 5;
    int cuts = pay(n);
    Console.Write(cuts +"\n");
      
    // Cuts Required in the
    // Length of 15
    n = 15;
    cuts = pay(n);
    Console.Write(cuts);
}
}
  
// This code is contributed by 29AjayKumar


输出:
2
3