📜  求 K 次交替减 X 加 Y 到 0 形成的数

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

求 K 次交替减 X 加 Y 到 0 形成的数

给定三个正整数KXY ,任务是找到通过交替减去X和将Y加到0总共K次所形成的数字。

例子:

朴素的方法:给定的问题可以通过执行给定的操作K次来解决并打印获得的结果。

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

高效方法:上述方法也可以通过在K次移动中找到递减(使用X的值)和递增(使用Y的值)的总值,然后打印这些值的总和作为结果来优化.

必须添加到结果中的值通过以下方式计算:

必须从结果中减去的值通过以下方式计算:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the value obtained
// after alternatively reducing X and
// adding Y to 0 total K number of times
int positionAfterKJumps(int X, int Y, int K)
{
    // Stores the final result after
    // adding only Y to 0
    int addY = Y * (K / 2);
 
    // Stores the final number after
    // reducing only X from 0
    int reduceX = -1 * X * (K / 2 + K % 2);
 
    // Return the result obtained
    return addY + reduceX;
}
 
// Driver Code
int main()
{
    int X = 2, Y = 5, K = 3;
    cout << positionAfterKJumps(X, Y, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the value obtained
    // after alternatively reducing X and
    // adding Y to 0 total K number of times
    static int positionAfterKJumps(int X, int Y, int K)
    {
       
        // Stores the final result after
        // adding only Y to 0
        int addY = Y * (K / 2);
 
        // Stores the final number after
        // reducing only X from 0
        int reduceX = -1 * X * (K / 2 + K % 2);
 
        // Return the result obtained
        return addY + reduceX;
    }
 
    // Driver Code
    public static void main(String[] args) {
         
        int X = 2, Y = 5, K = 3;
        System.out.print(positionAfterKJumps(X, Y, K));
    }
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
 
# Function to find the value obtained
# after alternatively reducing X and
# adding Y to 0 total K number of times
def positionAfterKJumps(X, Y, K):
   
    # Stores the final result after
    # adding only Y to 0
    addY = Y * (K // 2)
 
    # Stores the final number after
    # reducing only X from 0
    reduceX = -1 * X * (K // 2 + K % 2)
 
   # Return the result obtained
    return addY + reduceX
 
# Driver Code
X = 2
Y = 5
K = 3
print(positionAfterKJumps(X, Y, K))
 
# This code is contributed by subhammahato348.


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the value obtained
    // after alternatively reducing X and
    // adding Y to 0 total K number of times
    static int positionAfterKJumps(int X, int Y, int K)
    {
       
        // Stores the final result after
        // adding only Y to 0
        int addY = Y * (K / 2);
 
        // Stores the final number after
        // reducing only X from 0
        int reduceX = -1 * X * (K / 2 + K % 2);
 
        // Return the result obtained
        return addY + reduceX;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int X = 2, Y = 5, K = 3;
        Console.WriteLine(positionAfterKJumps(X, Y, K));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出:
1

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