📜  k次迭代后剩余的巧克力数量

📅  最后修改于: 2021-05-06 03:31:14             🧑  作者: Mango

给定一堆巧克力和一个整数“ k”,即迭代次数,任务是找到在k次迭代后剩余的巧克力数量。
注意:在每次迭代中,在剩下巧克力的平方根并吃掉其余部分之后,我们可以选择一堆巧克力数量最多的巧克力。
例子:

Input: Chocolates = 100000000, Iterations = 3
Output: 10

Input: Chocolates = 200, Iterations = 2
Output: 4

注意:如第二个示例中所示,在四舍五入该值后打印输出,输出约为3.76左右。
方法:
给出最大数量。选择了巧克力的数量,因此请考虑总堆积量,因为这将是最大的堆积量。
接下来,假设在每次迭代中只剩下巧克力的平方,因此考虑到

(((number)n)n)...n for k times = (number)nk

由于此处执行了k次平方根运算,因此(1/2) k由N供电。

考虑一亿个巧克力的例子,没有。的迭代次数是3,则将为

(((100000000)1/2)1/2)1/2 = (100000000)(1/2)3 = 10

以下是查找剩余巧克力所需的公式:

round(pow(n, (1.0/pow(2, k))))
C++
// C++ program to find remaining
// chocolates after k iterations
#include 
using namespace std;
  
// Function to find the chocolates left
int results(int n, int k)
{
    return round(pow(n, (1.0 / pow(2, k))));
}
  
// Driver code
int main()
{
    int k = 3, n = 100000000;
  
    cout << "Chocolates left after " << k
         << " iterations are " << results(n, k);
  
    return 0;
}


Java
// Java program to find remaining
// chocolates after k iterations
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
// Function to find the
// chocolates left
static int results(int n, int k)
{
    return (int)Math.round(Math.pow(n, 
           (1.0 / Math.pow(2.0, k))));
}
  
// Driver code
public static void main(String args[])
{
    int k = 3, n = 100000000;
  
    System.out.print("Chocolates left after " + k + 
               " iterations are " + results(n, k));
}
}
  
// This code is contributed by Subhadeep


C#
// C# program to find remaining
// chocolates after k iterations
using System;
  
class GFG
{
// Function to find the
// chocolates left
static int results(int n, int k)
{
    return (int)Math.Round(Math.Pow(n, 
           (1.0 / Math.Pow(2.0, k))));
}
  
// Driver code
public static void Main()
{
    int k = 3, n = 100000000;
  
    Console.Write("Chocolates left after " + 
                    k + " iterations are " + 
                             results(n, k));
}
}
  
// This code is contributed 
// by ChitraNayal


Python
# Python program to find 
# remaining chocolates 
# after k iterations
  
# Function to find the
# chocolates left
def results(n, k):
  
    return round(pow(n, (1.0 / 
                 pow(2, k))))
  
# Driver code
k = 3
n = 100000000
  
print("Chocolates left after"),
print(k),
print("iterations are"),
print(int(results(n, k)))
  
# This code is contributed
# by Shivi_Aggarwal


PHP


输出:
Chocolates left after 3 iterations are 10