0
Solve This
Jimmy finds 3 positive integers written on the ground, with spaces between them. Help Jimmy insert symbols in these spaces to make a valid equation! 1. There must be exactly one = . 2. The other symbol must be one of + , - ,* , / . Note: The division symbol / denotes real number division. For example, 3/2=1 is not a valid equation, but 3-2=1 and 3=2+1 are valid equations. Sample Input 13 2 15 Sample Output 13 +2 =15 Explanation This is a valid equation.
1 Answer
+ 3
https://code.sololearn.com/cxMma3yhBPv5
#include <iostream>
using namespace std;
//returns op such that a op b = c
char testOp(unsigned int a, unsigned int b, unsigned int c) {
char op;
if (a+b == c) {
op = '+';
} else if (a-b == c) {
op = '-';
} else if (a*b == c) {
op = '*';
} else if (double(a)/b == double(c)) {
op = '/';
} else {
op = ' ';
}
return op;
}
//prints the complete equation
void printEquation(unsigned int a, unsigned int b, unsigned int c) {
char op = testOp(a, b, c);
if (op != ' ') {
cout << a << op << b << "=" << c << endl;
} else {
op = testOp(b, c, a);
if (op != ' ') {
cout << a << "=" << b << op << c << endl;
} else {
cout << "Error: No match found." << endl;
}
}
}
int main() {
unsigned int a, b, c;
cin >> a >> b >> c;
printEquation(a, b, c);
return 0;
}