Created
May 9, 2015 07:40
-
-
Save azriel91/5b8519ac88aa6af49f4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Solution5 { | |
static int getValue(final int lastFunctionOperator, final String term) { | |
switch (lastFunctionOperator) { | |
case 0: | |
return Integer.parseInt(term); | |
case 1: | |
return Integer.parseInt(term); | |
case 2: | |
return -1 * Integer.parseInt(term); | |
} | |
return 0; // never reached | |
} | |
public static void main(String[] args) { | |
int max = (int) Math.pow(3, 8); | |
for (int n = 0; n < max; ++n) { | |
int sum = 0; | |
String sentence = ""; | |
String term = ""; | |
int lastFunctionOperator = 0; | |
for (int i = 1; i <=9 ; ++i) { | |
term += i; | |
// 0 = nothing | |
// 1 = plus | |
// 2 = subtract | |
int operator = (n / ((int) Math.pow(3, i - 1))) % 3; | |
switch (operator) { | |
case 0: | |
break; | |
default: | |
sum += getValue(lastFunctionOperator, term); | |
switch (lastFunctionOperator) { | |
case 0: break; | |
case 1: sentence += " + "; break; | |
case 2: sentence += " - "; break; | |
} | |
sentence += term; | |
term = ""; | |
lastFunctionOperator = operator; | |
break; | |
} | |
} // for i | |
sum += getValue(lastFunctionOperator, term); | |
switch (lastFunctionOperator) { | |
case 0: break; | |
case 1: sentence += " + "; break; | |
case 2: sentence += " - "; break; | |
} | |
sentence += term; | |
if (sum == 100) { | |
System.out.println(sentence + " = 100"); | |
} | |
} // for n | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment