📜  在给定条件下计算最大功率

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

给定3个整数N,M和K。任务是找到最大P ,使N * K P <= M。

例子:

方法:

在该算法中,简单地乘以n,其中K和更新N个与所述结果的电流值和由1增加可变功率(最初为0)。
为此,定义了一个具有2个基本情况的递归函数。

    1. 如果N的当前值大于M。此条件可以有两个条件:
    2. 最初,N大于所需的值,因此返回0
    3. 否则,返回功率– 1
  • 如果N的当前值等于M,则返回功率
  • 当前N 递归条件
    N更新为(N * k) ,将功率更新为当前功率+1

下面是上述方法的实现:

C++
// Compute maximum power to which K can be raised so
// that given condition remains true
#include 
using namespace std;
  
#define ll long long
  
// Function to return the largest
// power
int calculate(ll int n, ll int k,
              ll int m, ll int power)
{
  
    // If n is greater than given M
    if (n > m) {
        if (power == 0)
            return 0;
        else
            return power - 1;
    }
  
    // If n == m
    else if (n == m)
        return power;
  
    else
        // Checking for the next power
        return calculate(n * k, k, m, power + 1);
}
  
// Driver Code
int main()
{
    ll N = 1, K = 2, M = 5;
  
    cout << calculate(N, K, M, 0);
  
    return 0;
}


Java
// Java program for Compute maximum power 
// to which K can be raised so that 
// given condition remains true 
class GFG
{
  
// Function to return the largest 
// power 
static int calculate(int n, int k, 
                     int m, int power) 
{ 
  
    // If n is greater than given M 
    if (n > m)
    { 
        if (power == 0) 
            return 0; 
        else
            return power - 1; 
    } 
  
    // If n == m 
    else if (n == m) 
        return power; 
  
    else
        // Checking for the next power 
        return calculate(n * k, k, m, 
                          power + 1); 
} 
  
// Driver Code 
public static void main (String[] args) 
{ 
    int N = 1, K = 2, M = 5; 
  
    System.out.println(calculate(N, K, M, 0)); 
} 
}
  
// This code is contributed by AnkitRai01


Python
# Compute maximum power to
# which K can be raised so
# that given condition
# remains true
  
  
# Function to return the largest
# power
def calculate(n, k, m, power):     
      
    # If n is greater than given M                         
    if n > m:
        if power == 0:
            return 0
        else:
            return power-1
      
    # If n == m
    elif n == m:
        return power
    else:
        # Checking for the next power
        return calculate(n * k, k, m, power + 1)
  
# Driver's code     
if __name__=="__main__":
      
    N = 1
    K = 2
    M = 5
      
    print(calculate(N, K, M, 0))


C#
// C# program for Compute maximum power 
// to which K can be raised so that 
// given condition remains true 
using System;
class GFG
{
  
// Function to return the largest 
// power 
static int calculate(int n, int k, 
                     int m, int power) 
{ 
  
    // If n is greater than given M 
    if (n > m)
    { 
        if (power == 0) 
            return 0; 
        else
            return power - 1; 
    } 
  
    // If n == m 
    else if (n == m) 
        return power; 
  
    else
        // Checking for the next power 
        return calculate(n * k, k, m, 
                         power + 1); 
} 
  
// Driver Code 
public static void Main (String[] args) 
{ 
    int N = 1, K = 2, M = 5; 
  
    Console.WriteLine(calculate(N, K, M, 0)); 
} 
}
  
// This code is contributed by PrinciRaj1992


输出:
2