给定三个整数A , B和C。在一个操作中,选择三个整数中的任意两个,但条件是两个整数均应大于0,然后将其减小1 。任务是找到直到其中至少两个变为0为止可以执行的最大操作数。
例子:
Input: A = 1, B = 3, C = 1
Output: 2
Explanation:
Operation 1: Choose A and B, reduce both by 1. Current values: A = 0, B = 2, C = 1
Operation 2: Choose B and C, reduce both by 1. Current Values: A = 0, B = 1, C = 0
No more opeartions are possible as any pair chosen will have at least one 0 in it.
Input: A = 8, B = 1, C = 4
Output: 5
方法:想法是按照以下条件,以递减的顺序排列给定的数字,以找到最大操作数:
情况1:当A≥(B + C)
- 选择B对的A和B对。结果,在B操作之后,当前状态将为A =(A – B)和B = 0。
- 由于A≥(B + C)表示(A – B)≥C。因此可以选择A和C对来进行C操作,并且当前状态将为A =(A – B – C),B = 0 ,并且C = 0。
- 执行的总操作数= (B + C)
情况2:当A <(B + C)
- 执行一些操作后,尝试使A,B,C相等。
- 首先,使A和B相等。为此,选择A和C来执行(A – B)操作。将更新后的值命名为A 1 ,B 1和C 1 。值A 1 ,B 1和C 1将是:
A1 = A – (A – B)
B1 = B
C1 = C – (A – B)
- 执行的操作数= (A – B) 。
- A 1和B 1相等。因此,为(A 1 – C 1 )操作选择A 1和B 1对。
- 令A 2 ,B 2和C 2为上述操作之后的A,B和C的更新值。 A 2 ,B 2和C 2的值将相同,并且将是:
A2 = A1 – (A1 – C1) = C1 = (C – A + B)
B2 = C – A + B
C2 = C – A + B
- 假设截至目前已执行的操作总数为Z。因此Z的值将为:
Z = (A – B) + (A1 – C1) = (A – B) + (B – C + A – B)= 2A – B – C
- 由于A2 = B2 = C2,因此出现两种情况:
- A2,B2,C2甚至:有关对(A2,B2),(B2,C2),和(C2,A2)的A2,B2的计数,和C2每个组3个的操作由2减小。
令A2 = B2 = C2 =4。令可以执行的运算为X。因此,X =(4 + 4 + 4)/ 2 =6。因此,X的值可以概括为:
- A2,B2,C2甚至:有关对(A2,B2),(B2,C2),和(C2,A2)的A2,B2的计数,和C2每个组3个的操作由2减小。
- A2,B2,C2是奇数:关于对每一个组3个的操作(A2,B2),(B2,C2),和(C2,A2)的A2,B2的计数,和C2由2降低,终于,值A2,B2和C2分别达到1、1和1。在此可以执行一项附加操作。
令A2 = B2 = C2 =5。执行6次运算后,A2 = B2 = C2 =1。这里可以再进行一次运算。因此,可以执行的总操作数为7(6 + 1)。令可以执行的运算为Y。因此,Y = floor((5 + 5 + 5)/ 2)=7。因此,Y的值可以概括为:
- 由于从上述步骤X = Y,因此可以通过以下方式给出可能原因的总数:
Total number of possible cases = (Z + X) = (Z + Y) = (A + B + C) / 2.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// operations
int solution(int A, int B, int C)
{
int arr[3];
// Insert the three numbers in array
arr[0] = A, arr[1] = B, arr[2] = C;
// Sort the array
sort(arr, arr + 3);
// Case 2
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1]
+ arr[2])
/ 2);
// Case 1
else
return (arr[0] + arr[1]);
}
// Driver Code
int main()
{
// Given A, B, C
int A = 8, B = 1, C = 5;
// Function Call
cout << solution(A, B, C);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum number
// operations
public static int solution(int A, int B, int C)
{
int arr[] = new int[3];
// Insert the three numbers in array
arr[0] = A; arr[1] = B; arr[2] = C;
// Sort the array
Arrays.sort(arr);
// Case 2
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1] + arr[2]) / 2);
// Case 1
else
return (arr[0] + arr[1]);
}
// Driver Code
public static void main(String[] args)
{
// Given A, B, C
int A = 8, B = 1, C = 5;
// Function call
System.out.println(solution(A, B, C));
}
}
// This code is contributed by jrishabh99
Python
#Python3 program for the above approach
#Function to find the minimum number
#operations
def solution(A, B, C):
arr=[0] * 3
#Insert the three numbers in array
arr[0] = A
arr[1] = B
arr[2] = C
#Sort the array
arr = sorted(arr)
#Case 2
if (arr[2] < arr[0] + arr[1]):
return ((arr[0] + arr[1] + arr[2]) // 2)
#Case 1
else:
return (arr[0] + arr[1])
#Driver Code
if __name__ == '__main__':
#Given A, B, C
A = 8
B = 1
C = 5
#Function Call
print(solution(A, B, C))
# This code is contributed by Mohit Kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// operations
public static int solution(int A, int B, int C)
{
int []arr = new int[3];
// Insert the three numbers in array
arr[0] = A; arr[1] = B; arr[2] = C;
// Sort the array
Array.Sort(arr);
// Case 2
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1] + arr[2]) / 2);
// Case 1
else
return (arr[0] + arr[1]);
}
// Driver Code
public static void Main(String[] args)
{
// Given A, B, C
int A = 8, B = 1, C = 5;
// Function call
Console.WriteLine(solution(A, B, C));
}
}
// This code is contributed by Rajput-Ji
6
时间复杂度: O(1)
空间复杂度: O(1)