解决这个密码算术程序会产生无限的结果



我正在做EAT+ THAT= APPLE,其中每个字母代表0-9的不同数字。我需要找到所有组合。我想知道是否有更好的写法,尤其是"如果"和"为">

我试过这样写,但它给了我无限的结果

public class Main {

public static void main(String[] args) {
int count = 0;
int E,A,T,P,L,H;
for (E = 0; E <=9; E++)   
{
for (A = 0; A <=9; A++)
for (T = 0; T <=9; T++)
for (P = 0; P <=9; P++)
for (L = 0; L <=9; L++)
for (H = 0; H <=9; H++)
if (((E != A) && (E != L) && (E != T)&&(E !=P) &&(E!=L)&&(E!=H) && 
(T != A) && (T != L) && (T != E) &&(T!=P)&&(T!=L)&&(T!=H)))
{
System.out.println("A"+A+"P"+P+"P"+P+"L"+L+"E"+E);
}
else count = count +1;    
}
System.out.println(count);
}
}

当面对这些问题时,尽可能简化问题非常重要。

让我们创建一个子问题:

假设THAT必须等于 8208。每个角色的价值是什么?

你可以注意到你只是在求解一个方程T* 1000 +H* 100 +A* 10 +T= 8208 (唯一的解决方案是T= 8,H= 2,A= 0)

回到我们的主要问题:

使用上面的逻辑将主要问题简化为一个方程

EAT+THAT=APPLE=>EAT+THAT-APPLE= 0;

这实际上意味着:

E* 100 +A* 10 +T+T* 1000 +H* 100 +A* 10 +T-A* 10000 -P

* 1000 -P* 1000 -L* 10 -E= 0

简化后,您将获得: -9980 *A- 1100 *P+ 1002 *T+ 100 *H+ 99 *E- 10 *L= 0

由于每个变量的值非常有限,我们可以自由地暴力破解解决方案。

public class MyClass {
public static int[] calc(){
for(int A = 0; A < 10; A++){
for(int P = 0; P < 10; P++){
for(int T = 0; T < 10; T++){
for(int H = 0; H < 10; H++){
for(int E = 0; E < 10; E++){
for(int L = 0; L < 10; L++){
if(A!=P && A != T && A != H && A != E && A != L && P != T && P != H && P != E && P != L && T != H && T != E && T != L && H != E && H != L && E != L){
//In your code you are lacking this statment, it checks whether values are indeed a solution of the equation
if(-9980 * A - 1100 * P + 1002 * T + 100 * H + 99 * E - 10 * L == 0){
int[] outArr = {A, P, T, H, E, L};
return outArr;
}
}
}
}
}
}
}
}
return null;
}

public static void main(String args[]) {
int[] answer = calc();
System.out.println("A" + answer[0] + " P" + answer[1] + " P" + answer[1] + " L" + answer[5] + " E" + answer[4]);
}
}

如果你不希望你的if语句如此庞大,你总是可以创建一个大小为10的数组,用零填充,对于每个变量(我们称之为i)用array[i]++;增加给定索引的值,如果在任何时候在任何索引array[x]>1,你都会知道值重复。

有一些方法可以通过确认数字之间的关系来优化脚本的工作方式(例如,您可以观察到A只能等于 0 或 1,而 0 会导致一个矛盾的方程,因此A必须等于 1,依此类推,直到您仅使用逻辑找到每个数字的确切值), 但最终你会得到纯粹的数学解决方案,我不认为这是你想要的。

**Second Version of Code**
public class EatingApple {
ArrayList<Permutation>  eat = new ArrayList();
static      ArrayList<Permutation> that = new ArrayList();
static     ArrayList<Permutation> apples = new ArrayList();

public static void main(String[] args) throws IOException {
EatingApple apples  = new EatingApple();
apples.makePermutation();
apples.searchAppleEqualsSum();



}
public void searchAppleEqualsSum() 
{
for(Permutation eatP : eat)
{
int E_eat = (Integer) eatP.getCharacterValue("E");
int A_eat = (Integer) eatP.getCharacterValue("A");
int T_eat = (Integer) eatP.getCharacterValue("T");
for(Permutation thatP : that)
{
int T_that = (Integer) thatP.getCharacterValue("T");
int A_that = (Integer) thatP.getCharacterValue("A");
if(T_eat == T_that&&A_eat ==A_that)
for(Permutation apple : apples)
{
int A_apple = (Integer) apple.getCharacterValue("A");
int E_apple = (Integer) apple.getCharacterValue("E");
if(A_apple==E_eat&&E_apple==E_eat)
{
int  eat_value = Integer.parseInt(eatP.permutationString);
int  that_value = Integer.parseInt(thatP.permutationString);
int  apple_value = Integer.parseInt(apple.permutationString);
if(apple_value == (that_value + eat_value)&&apple_value!=0)
{
System.out.println("EAT :" + eatP.permutationString);
System.out.println("THAT :" + thatP.permutationString);
System.out.println("Apple :" + apple.permutationString);
System.out.println(".............");
}
}
}



}

}
}
public void makePermutation()
{
for(int e=0;e<10;e++)
for(int a=0;a<10;a++)
for(int t=0;t<10;t++)
{
String permutationString = ""+e+a+t;
int value = e+a+t;
Permutation eatCombination = new Permutation(permutationString,value);
eatCombination.addCharToMap("E", e);
eatCombination.addCharToMap("A", a);
eatCombination.addCharToMap("T", t);
eatCombination.permutationValue=value;


eat.add(eatCombination);

}

for (int t = 0; t < 10; t++)
for (int h = 0; h < 10; h++) 
for(int a = 0; a < 10;a++)
{
String permutationString = ""+t+h+a+t;
int value = t + h + a + t;

Permutation thatCombination = new Permutation(permutationString,value);


thatCombination.addCharToMap("T", t);
thatCombination.addCharToMap("H", h);
thatCombination.addCharToMap("A", a);
thatCombination.permutationValue=value;

that.add(thatCombination);

}
for (int a = 0; a < 10; a++)
for (int p = 0; p  < 10; p++) 
for(int l = 0; l <10;l++)
for(int e = 0; e < 10; e++)

{
String permutationString = ""+a+p+p+l+e;
int value = a+p+p+l+e;


Permutation appleCombination = new Permutation(permutationString,value);

appleCombination.addCharToMap("A", a);
appleCombination.addCharToMap("P", p);
appleCombination.addCharToMap("L", l);
appleCombination.addCharToMap("E", e);

appleCombination.permutationValue=value;

apples.add(appleCombination);


}


}
class Permutation
{
String permutationString="";
int permutationValue =0;
HashMap wordMap;
public int getPermuttionValue() {
return permutationValue;
}
Permutation(String pString, int pValue)
{
this.wordMap = new HashMap();
this.permutationString=pString;
this.permutationValue=pValue;
}
public int getCharacterValue(String character)
{
if(wordMap.get(character)==null)
return -1;
return (Integer) wordMap.get(character);


}
public  void  addCharToMap(String character, int value)
{

wordMap.put(character, value);
}


}
}

最新更新