#P1689. Solve the Missing Number in an Equation
Solve the Missing Number in an Equation
Solve the Missing Number in an Equation
You are given an equation in one of the forms \(X+Y=Z\) or \(X-Y=Z\). Two of the three numbers are provided and one of them is replaced by a question mark (?
). Note that the equation may contain extra spaces.
Your task is to find the missing number. For instance, if the equation is ? + 2 = 3
, then the missing number is 1 because \(1 + 2 = 3\).
The rules are as follows:
- If the missing number is in the place of \(X\) and the equation is \(?+Y=Z\), then compute \(X = Z - Y\).
- If the missing number is in the place of \(Y\) and the equation is \(X+?=Z\), then compute \(Y = Z - X\).
- If the missing number is in the place of \(Z\) and the equation is \(X+Y=?\), then compute \(Z = X + Y\).
- If the missing number is in \(X\) for the equation \(?-Y=Z\), then compute \(X = Z + Y\).
- If the missing number is in \(Y\) for the equation \(X-?=Z\), then compute \(Y = X - Z\).
- If the missing number is in \(Z\) for the equation \(X-Y=?\), then compute \(Z = X - Y\).
inputFormat
The input consists of a single line containing the equation with potential extra spaces. The numbers are integers, and one of the three numbers is replaced by a question mark (?
).
outputFormat
Output the integer value of the missing number.
sample
? + 2 = 3
1