给定两个整数N和K ,其中K表示我们被允许直接从N减少N到N – K的跳跃数,我们的任务是按照给定的操作计算最小步数达到0:
- 我们可以从N跳出K个量,即N = N – K
- 将N减1,即N = N -1。
例子:
Input: N = 11, K = 4
Output: 5
Explanation:
For the given value N we can perform the operation in the given sequence: 11 -> 7 -> 3 -> 2 -> 1 -> 0
Input: N = 6, K = 3
Output: 2
Explanation:
For the given value N we can perform the operation in the given sequence: 6 -> 3 -> 0.
方法:
为了解决上述问题,我们知道,将需要N / K的步长直接从值N跳到具有K和N%K的步长的最小可分值,以将其递减1,以将计数减少到0。因此,总和从N达到0所需的步数为
(N / K) + (N % K)
下面是上述方法的实现:
C++
// C++ program to Count the minimum steps
// to reach 0 from the given integer N
#include
using namespace std;
// Function retuns min step
// to reach 0 from N
int getMinSteps(int n, int jump)
{
// Direct possible
// reduction of value N
int quotient = n / jump;
// Remaining steps needs
// to be reduced by 1
int remainder = n % jump;
// Summation of both the values
int steps = quotient + remainder;
// Return the final answer
return steps;
}
// Driver code
int main()
{
int N = 6, K = 3;
cout << getMinSteps(N, K);
return 0;
}
Java
// Java program to count the minimum steps
// to reach 0 from the given integer N
class GFG{
// Function retuns min step
// to reach 0 from N
static int getMinSteps(int n, int jump)
{
// Direct possible
// reduction of value N
int quotient = n / jump;
// Remaining steps needs
// to be reduced by 1
int remainder = n % jump;
// Summation of both the values
int steps = quotient + remainder;
// Return the final answer
return steps;
}
// Driver code
public static void main(String[] args)
{
int N = 6, K = 3;
System.out.print(getMinSteps(N, K));
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program to Count the minimum steps
# to reach 0 from the given integer N
# Function retuns min step
# to reach 0 from N
def getMinSteps(n, jump):
# Direct possible
# reduction of value N
quotient = int(n / jump)
# Remaining steps needs
# to be reduced by 1
remainder = n % jump
# Summation of both the values
steps = quotient + remainder
# Return the final answer
return steps
# Driver code
N = 6
K = 3
print (getMinSteps(N, K))
# This code is contributed by PratikBasu
C#
// C# program to count the minimum steps
// to reach 0 from the given integer N
using System;
class GFG{
// Function retuns min step
// to reach 0 from N
static int getMinSteps(int n, int jump)
{
// Direct possible
// reduction of value N
int quotient = n / jump;
// Remaining steps needs
// to be reduced by 1
int remainder = n % jump;
// Summation of both the values
int steps = quotient + remainder;
// Return the final answer
return steps;
}
// Driver code
public static void Main(string[] args)
{
int N = 6, K = 3;
Console.Write(getMinSteps(N, K));
}
}
// This code is contributed by rutvik_56
Javascript
输出:
2