📌  相关文章
📜  最大化 N 笔交易可能的不同利润的数量

📅  最后修改于: 2021-10-26 05:09:23             🧑  作者: Mango

给定三个整数N、XY ,分别代表交易总数、最小利润和最大利润,任务是使用XY找出可以在N ( N > 1 ) 次交易中赚取的不同总利润的计数至少一次。

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:

  • 打印值(S2-S1+1)。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count distinct
// profits possible
int numberOfWays(int N, int X, int Y)
{
    // Stores the minimum total profit
    int S1 = (N - 1) * X + Y;
 
    // Stores the maximum total profit
    int S2 = (N - 1) * Y + X;
 
    // Return count of distinct profits
    return (S2 - S1 + 1);
}
 
// Driver code
int main()
{
    // Input
    int N = 3;
    int X = 13;
    int Y = 15;
 
    // Function call
    cout << numberOfWays(N, X, Y);
    return 0;
}


Java
// Java program for the above approach
 
import java.util.Arrays;
 
class GFG
{
 
// Function to count distinct
// profits possible
static int numberOfWays(int N, int X, int Y)
{
    // Stores the minimum total profit
    int S1 = (N - 1) * X + Y;
 
    // Stores the maximum total profit
    int S2 = (N - 1) * Y + X;
 
    // Return count of distinct profits
    return (S2 - S1 + 1);
}
 
 
// Driver code
public static void main(String[] args)
{
    // Input
    int N = 3;
    int X = 13;
    int Y = 15;
 
    // Function call
    System.out.println(numberOfWays(N, X, Y));
    }
}
 
// This code is contributed by jana_sayantan.


Python3
# Python3 program for the above approach
 
# Function to count distinct
# profits possible
def numberOfWays(N, X, Y):
     
    # Stores the minimum total profit
    S1 = (N - 1) * X + Y
 
    # Stores the maximum total profit
    S2 = (N - 1) * Y + X
 
    # Return count of distinct profits
    return (S2 - S1 + 1)
 
# Driver code
if __name__ == '__main__':
     
    # Input
    N = 3
    X = 13
    Y = 15
 
    # Function call
    print(numberOfWays(N, X, Y))
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
         
class GFG
{
 
// Function to count distinct
// profits possible
static int numberOfWays(int N, int X, int Y)
{
    // Stores the minimum total profit
    int S1 = (N - 1) * X + Y;
 
    // Stores the maximum total profit
    int S2 = (N - 1) * Y + X;
 
    // Return count of distinct profits
    return (S2 - S1 + 1);
}
     
// Driver Code
public static void Main()
{
    // Input
    int N = 3;
    int X = 13;
    int Y = 15;
 
    // Function call
    Console.WriteLine(numberOfWays(N, X, Y));
     
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
3

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