Created
December 25, 2010 12:32
-
-
Save bngsudheer/754855 to your computer and use it in GitHub Desktop.
That Anta Heli solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ctype.h> | |
/** | |
Author - Sudheer Satyanarayana, http://techchorus.net | |
All rights reserved | |
Solution for That Anta Heli problem | |
a, b, c, d and r are given as input | |
Solve the problem | |
a ? b ? c ? d = r | |
find ?, ? and ? where ? is either +, -, * or / | |
The equation should be treated as | |
(( (a ? b) ? c) ? d) = r | |
Example problem: 45 ? 23 ? 34 ? 1 = 33 | |
Solution : 45 + 23 - 34 - 1 = 33 | |
Thus the solution is + - - | |
**/ | |
int operate(int x, int y, char operator) | |
{ | |
if (operator == '+') { | |
return x + y; | |
} | |
if (operator == '-') { | |
return x - y; | |
} | |
if (operator == '*') { | |
return x * y; | |
} | |
if (operator == '/') { | |
return x / y; | |
} | |
} | |
find_solution(int a, int b, int c, int d, int r) | |
{ | |
char first[4] = {'+', '-', '*', '/'}; | |
char second[4] = {'+', '-', '*', '/'}; | |
char third[4] = {'+', '-', '*', '/'}; | |
char solution[3]; | |
int r1, r2, r3; | |
int i, j, k; | |
for (i = 0; i < 4; ++i) { | |
for (j = 0; j < 4; ++j) { | |
for (k = 0; k < 4; ++k) { | |
r1 = operate(a, b, first[i]); | |
r2 = operate(r1, c, second[j]); | |
r3 = operate(r2, d, third[k]); | |
if (r3 == r) { | |
printf("\nFound solution %d %c %d %c %d %c %d = %d", a, first[i], b, second[j], c, third[k], d, r); | |
return; | |
} | |
} | |
} | |
} | |
} | |
main() | |
{ | |
int a = 45; | |
int b = 23; | |
int c = 34; | |
int d = 1; | |
int r = 33; | |
find_solution(a, b, c, d, r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment