아마도 대부분의 사람들이 Send + More = Money를 알고 있습니다. 글쎄요, 저는 현재 자바를 배우고 있고 연습 문제 중 하나는 HES + THE = BEST를 해결해야한다는 것입니다.구두 산술/알파벳에 대한보다 효율적인 접근 방법은 무엇입니까?
지금까지는 if-for-do-do 루프를 사용할 수 있습니다. 그것을 해결할 수있는 여러 가지 방법이 있다고 확신하지만, 그것은 제가 경험하고있는 운동의 요점이 아닙니다. if-for-do-do 루프를 가장 효율적인 방법으로 사용할 수 있어야합니다.
내 문제는 무엇입니까? 나는 그것을 해결하는 효율적인 방법을 생각할 수 없다! 그것은이 퍼즐을 해결하기 위해 총 약 15,000 이상한 루프 사이클 소요
public class Verbalarithmetics {
public static void main (String args[]) {
// Countint Variables
int index_h = 0;
int index_e = 0;
int index_s = 0;
int index_t = 0;
int index_b = 0;
// Start with h = 1 and increase until the else-if statement is true
for(int h = 1; h <= 9; h++) { // h = 1, because first Symbol can't be zero
index_h++;
// Increase e so long until e equals h
for(int e = 0; e <= 9; e++) {
index_e++;
if (e == h) {
continue;
}
// Increase s so long until s equals h or e
for(int s = 0; s <= 9; s++) {
index_s++;
if (s == h || s == e) {
continue;
}//end if
// Increase t so long until t equals h or e or s.
for(int t = 1; t <= 9; t++) { // t = 1, because 1st Symbol cant be zero
index_t++;
if(t == h || t == e || t == s) {
continue;
}// end if
// Increase b so long until b equals h, e, s or t.
for(int b = 1; b <= 9; b++) { // b = 1, weil das 1. Symbol nicht für eine 0 stehen darf
index_b++;
if (b == h || b == e || b == s || b == t) {
continue;
}// end if
// x = 100*h + 10*e + s
// y = 100*t + 10*h + e
// z = 1000*b + 100*e + 10*s + t
// Check if x+y=z, if true -> Print out Solution, else continue with the upper most loop
else
if (100*h + 10*e + s + 100*t + 10*h + e == 1000*b + 100*e +10*s + t) {
System.out.println("HES + THE = BEST => " + h + e + s + " + " + t + h + e + " = " + b + e + s + t);
System.out.println("With H=" + h + ", E=" + e + ", S=" + s + ", T=" + t + ", und B=" + b + ".");
System.out.println("It took " + index_h +
" Loop-Cycles to find 'h' !");
System.out.println("It took " + index_e +
" Loop-Cycles to find 'e' !");
System.out.println("It took " + index_s +
" Loop-Cycles to find 's' !");
System.out.println("It took " + index_t +
" Loop-Cycles to find 't' !");
System.out.println("It took " + index_b +
" Loop-Cycles to find 'b' !");
System.out.println("This is a total of " + (index_h + index_e + index_s + index_t + index_b) +
" Loop-Cycles");
}// end else if
}//end for
}//end for
}//end for
}//end for
}//end for
}
}
: 나는 퍼즐을 해결, 그러나 아마 그렇게 할 수있는 최악의 효율적인 방법 인이 함께 왔어요. 그것은 내 의견으로는 많이있다. 어떤 포인터라도주세요?
편집자님께 : "숙제"를 태그로 추가하지 않으시겠습니까? 숙제가 아닙니다. 이것은 자기 운동입니다. 뭐? 나는 책을 읽을 수없고 그것에 따라 운동을합니까? 숙제가 있어야합니까? ... 진지하게 그만하십시오.
** ** ** 진짜 질문입니다. 투표했다. – ChssPly76