📜  通过加或减 D 使 K 步中 N 的绝对值最小化

📅  最后修改于: 2022-05-13 01:56:05.457000             🧑  作者: Mango

通过加或减 D 使 K 步中 N 的绝对值最小化

给定三个数NDK ,任务是在应用K 次操作后找到N的最小绝对值。在一次操作中,可以将N更改为N+DND

例子:

方法:要获得最小值,请尝试将N移动到尽可能接近 0。在N处,计算N/D给出接近 0 所需的移动次数。如果K小于N/D ,则使用所有移动来接近 0 ,即使用所有移动将N更改为ND 。否则,如果(K > N/D) ,所有K移动不能在一个方向上使用,因为它超过 0,然后会增加N的值。所以首先使用N/D移动然后我们接近 0。现在KK = K - N/D使用N/D之后。现在,来回使用这种技术,找到答案。请按照以下步骤解决此问题:

  1. 首先,取 N 的绝对值,因为从0-NN具有相同的距离。
  2. 初始化变量并计算totalMoves = N/D
  3. 如果K小于totalMoves ,则从N中减去D*K
  4. 否则,如果K大于totalMoves ,则从N中减去D*K ,从K中减去totalMoves
  5. 现在,检查K是否为奇数,从N中减去D。
  6. 否则按原样打印。
C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum absolute value
// of N after applying K operations
int minAbsValue(int N, int K, int D)
{
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
        N = N - K * D;
    }
    else {
 
        // Takes all moves which
        // goes closer to 0.
        N = N - totalMoves * D;
 
        // Subtract total moves we use
        K = K - totalMoves;
 
        // Now, check K, If K is odd
        // we take only one move
        if (K & 1) {
            N = abs(N - D);
        }
    }
 
    return N;
}
 
// Driver Code
int main()
{
 
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    cout << ans;
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the minimum absolute value
  // of N after applying K operations
  static int minAbsValue(int N, int K, int D)
  {
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
      N = N - K * D;
    }
    else {
 
      // Takes all moves which
      // goes closer to 0.
      N = N - totalMoves * D;
 
      // Subtract total moves we use
      K = K - totalMoves;
      K = K - totalMoves;
 
      // Now, check K, If K is odd
      // we take only one move
      if (K % 2 == 1) {
        N = Math.abs(N - D);
      }
    }
 
    return N;
  }
  public static void main (String[] args) {
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    System.out.print(ans);
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# python3 program for the above approach
 
# Function to find the minimum absolute value
# of N after applying K operations
def minAbsValue(N, K, D):
 
    # Take totalMoves we need
    # from N to (closer to 0)
    totalMoves = N // D
 
    # totalMoves is greater than
    # K use all the moves
    if (totalMoves >= K):
        N = N - K * D
 
    else:
 
        # Takes all moves which
        # goes closer to 0.
        N = N - totalMoves * D
 
        # Subtract total moves we use
        K = K - totalMoves
 
        # Now, check K, If K is odd
        # we take only one move
        if (K & 1):
            N = abs(N - D)
 
    return N
 
# Driver Code
if __name__ == "__main__":
 
    N, K, D = 6, 2, 4
    ans = minAbsValue(N, K, D)
    print(ans)
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
  // Function to find the minimum absolute value
  // of N after applying K operations
  static int minAbsValue(int N, int K, int D)
  {
 
    // Take totalMoves we need
    // from N to (closer to 0)
    int totalMoves = N / D;
 
    // totalMoves is greater than
    // K use all the moves
    if (totalMoves >= K) {
      N = N - K * D;
    }
    else {
 
      // Takes all moves which
      // goes closer to 0.
      N = N - totalMoves * D;
 
      // Subtract total moves we use
      K = K - totalMoves;
      K = K - totalMoves;
 
      // Now, check K, If K is odd
      // we take only one move
      if (K % 2 == 1) {
        N = Math.Abs(N - D);
      }
    }
 
    return N;
  }
  public static void Main () {
    int N = 6, K = 2, D = 4;
    int ans = minAbsValue(N, K, D);
    Console.Write(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

时间复杂度: O(1)
辅助空间: O(1)