📜  计算将C分为两部分并加到A和B以使A严格大于B的方法

📅  最后修改于: 2021-05-04 07:16:19             🧑  作者: Mango

给定三个整数A,BC ,任务是计算将C分为两部分并加到AB的方式的数量,以使A严格大于B。

例子:

方法:仔细观察后,针对该问题形成以下关系。

  • ADDAADDB分别添加到AB.
  • 因此, addA + addB = C且应满足不等式A + addA> B + addB
  • 现在,由于addB = C – addA并将其放在不等式中:
    A + addA > B + (C - addA)
    or, 2addA > C + B - A
    or, 2addA >= C + B - A + 1
    or, addA >= (C + B - A + 1) / 2
    
  • 由于addA必须为非负数,因此addA = max(0,(C + B – A + 1)/ 2)
  • 该划分应为上限划分,因此我们可以将其重写为addA = max(0,(C + B – A + 2)/ 2)。
  • 令该值等于minAddA 。由于[minAddA,C]中的所有整数值addA都满足关系A + addA> B + addB ,因此所需的方式数等于max(0,C – minAddA + 1)

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to count the number of ways to divide
// C into two parts and add to A and B such
// that A is strictly greater than B
int countWays(int A, int B, int C)
{
    // Minimum value added to A to satisfy
    // the given relation
    int minAddA = max(0, (C + B - A + 2) / 2);
  
    // Number of different values of A, i.e.,
    // number of ways to divide C
    int count_ways = max(C - minAddA + 1, 0);
  
    return count_ways;
}
  
// Driver code
int main()
{
    int A = 3, B = 5, C = 5;
  
    cout << countWays(A, B, C);
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
  
class GFG{
  
    // Function to count the number of ways to divide
    // C into two parts and add to A and B such
    // that A is strictly greater than B
    static int countWays(int A, int B, int C)
    {
        // Minimum value added to A to satisfy
        // the given relation
        int minAddA = Math.max(0, (C + B - A + 2) / 2);
      
        // Number of different values of A, i.e.,
        // number of ways to divide C
        int count_ways = Math.max(C - minAddA + 1, 0);
      
        return count_ways;
    }
      
    // Driver code
    public static void main(String args[])
    {
        int A = 3, B = 5, C = 5;
      
        System.out.println(countWays(A, B, C));
    }
}
  
// This code is contributed by AbhiThakur


Python3
# Python3 implementation of the above approach
  
# Function to count the number of ways to divide
# C into two parts and add to A and B such
# that A is strictly greater than B
def countWays(A, B, C):
      
    # Minimum value added to A to satisfy
    # the given relation
    minAddA = max(0, (C + B - A + 2) // 2)
      
    # Number of different values of A, i.e.,
    # number of ways to divide C
    count_ways = max(C - minAddA + 1, 0)
      
    return count_ways
  
# Driver code
A = 3
B = 5
C = 5
print(countWays(A, B, C))
  
# This code is contributed by shivanisingh


C#
// C# implementation of the above approach
using System;
   
class GFG
{
  
// Function to count the number of ways to divide
// C into two parts and add to A and B such
// that A is strictly greater than B
static int countWays(int A, int B, int C)
{
    // Minimum value added to A to satisfy
    // the given relation
    int minAddA = Math.Max(0, (C + B - A + 2) / 2);
  
    // Number of different values of A, i.e.,
    // number of ways to divide C
    int count_ways = Math.Max(C - minAddA + 1, 0);
  
    return count_ways;
}
  
// Driver Code
public static void Main(String[] args)
{
    int A = 3, B = 5, C = 5;
  
    Console.Write(countWays(A, B, C));
}
  
}
  
// This code is contributed by shivanisinghss2110


输出:
2