Hat dir diese Antwort geholfen?
Brute Force geht auch glaub ich jedenfalls, ausprobiert hab ich es nicht, das dauert mir zu lange...
package lisaa.main;
import java.math.BigInteger;
public class BruteForce {
private static final String operand1 = "DREI";
private static final String operand2 = "DREI";
private static final String operand3 = "VIER";
private static final String result = "ZEHN";
private static final BigInteger MAX = new BigInteger(
"99999999999999999999999999");
private static final BigInteger MILLION = new BigInteger(
"1000000");
public static void main(String [ ] args) {
BigInteger i = BigInteger.ZERO;
while (i.compareTo(MAX) != 1) {
i = i.add(BigInteger.ONE);
if (test(i)) {
return;
}
}
System.out.println("Keine Lösung");
}
private static boolean test(BigInteger integer) {
char[] dictionary = print(integer).toCharArray();
try {
if (integer.divideAndRemainder(MILLION)[1].equals(BigInteger.ZERO)) {
System.out.println(dictionary);
}
} catch (ArithmeticException e) {
// be quiet
}
String o1 = operand1;
String o2 = operand2;
String o3 = operand3;
String r = result;
int i = 65;
for (char c : dictionary) {
char x = (char)i;
if (x == 'E') {
c = '8';
}
o1 = o1.replace(x, c);
o2 = o2.replace(x, c);
o3 = o3.replace(x, c);
r = r.replace(x, c);
i++;
}
int i1 = Integer.valueOf(o1);
int i2 = Integer.valueOf(o2);
int i3 = Integer.valueOf(o3);
int i4 = Integer.valueOf(r);
if (i1 + i2 + i3 == i4) {
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
return true;
}
return false;
}
private static String print(BigInteger integer) {
String s = integer.toString();
String rval = "";
for (int i = 0; i < (26 - s.length()); i++) {
rval += "0";
}
return rval + s;
}
}