📜  N可减去最大完美立方的次数

📅  最后修改于: 2021-04-23 05:37:01             🧑  作者: Mango

给定数字N ,请在每一步中,从N中减去最大的理想立方(≤N)。在N> 0时重复此步骤。任务是计算可以执行的步骤数。

例子:

方法

  • 获取必须从中减少最大的理想立方体的数量。
  • 查找数字的立方根,并将结果转换为整数。数字的立方根可能在小数点后包含小数部分,需要避免。
  • 减去上一步中找到的整数的立方。这将在上述步骤中从数字中删除最大可能的完美立方体。
N = N - ((int) ∛N)3
  • 减少数量重复上述两个步骤,直到大于0。
  • 打印一个理想的多维数据集从N减少的次数。这是最终结果。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to return the count of steps
int countSteps(int n)
{
 
    // Variable to store the count of steps
    int steps = 0;
 
    // Iterate while N > 0
    while (n) {
 
        // Get the largest perfect cube
        // and subtract it from N
        int largest = cbrt(n);
        n -= (largest * largest * largest);
 
        // Increment steps
        steps++;
    }
 
    // Return the required count
    return steps;
}
 
// Driver code
int main()
{
    int n = 150;
    cout << countSteps(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG{
  
// Function to return the count of steps
static int countSteps(int n)
{
  
    // Variable to store the count of steps
    int steps = 0;
  
    // Iterate while N > 0
    while (n > 0) {
  
        // Get the largest perfect cube
        // and subtract it from N
        int largest = (int) Math.cbrt(n);
        n -= (largest * largest * largest);
  
        // Increment steps
        steps++;
    }
  
    // Return the required count
    return steps;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 150;
    System.out.print(countSteps(n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
from math import floor
 
# Function to return the count of steps
def countSteps(n):
 
    # Variable to store the count of steps
    steps = 0
 
    # Iterate while N > 0
    while (n):
 
        # Get the largest perfect cube
        # and subtract it from N
        largest = floor(n**(1/3))
        n -= (largest * largest * largest)
 
        # Increment steps
        steps += 1
 
    # Return the required count
    return steps
 
# Driver code
n = 150
print(countSteps(n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG{
   
// Function to return the count of steps
static int countSteps(int n)
{
   
    // Variable to store the count of steps
    int steps = 0;
   
    // Iterate while N > 0
    while (n > 0) {
   
        // Get the largest perfect cube
        // and subtract it from N
        int largest = (int) Math.Pow(n,(double)1/3);
        n -= (largest * largest * largest);
   
        // Increment steps
        steps++;
    }
   
    // Return the required count
    return steps;
}
   
// Driver code
public static void Main(String[] args)
{
    int n = 150;
    Console.Write(countSteps(n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
5