java.lang.StackOverflowError in the use of
-
Hello. The following code does not work (work at a lower value s1):
public class Class1 { static String[] s = { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; //Тут хранятся символы
public static void main(String[] args) { long s1 = 37090594994L; //Данное число for (int i = 1; i < 27; i++) { //Здесь подбирается последняя буква che(s1 - i, s[i], 0); //Для предпоследней буквы } } private static void che(long l, String str, int mult) { mult++; // Степень, в которую возводится 24 for (int i = 1; i < 27; i++) { //подбираем следующую букву long tmp = i * mult(mult); //следующий член многочлена if (l - tmp == 0) //если больше не найти букв System.out.println(s[i] + str); //выводим else if (l - tmp > 1) //а если можно найти ещё одну букву che(l - tmp, s[i] + str, mult); //то ищем её } } private static long mult(int mult) { //возведение в степень int result = 1; for (int i = 1; i <= mult; i++) result *= 24; return result; }
}
The compiler shall remove the following:
Exception in thread "main" java.lang.StackOverflowError
at java.util.Arrays.copyOfRange(Arrays.java:2691)
at java.lang.String.<init>(String.java:203)
at java.lang.StringBuilder.toString(StringBuilder.java:405)
at Class1.che(Class1.java:23)
at Class1.che(Class1.java:23)
at Class1.che(Class1.java:23)
at Class1.che(Class1.java:23) и так далее сотню раз...
The essence of the assignment is:
Accrued password s is the number
x(s)= s[1]*24^(n-1) + s[2]*24^(n-2) + ... + s[n-1]*24 +
s[n] where s[i] is the code of the letter of the password s standing on the i-place. Books
coded in row in the English alphabet,
the letter A is coded by unit, B is double, etc. Help!
Approve the password if the assigned number 37090594994.Thank you so much!
-
Krch, the answer to the question why it goes so far is the method of calculation:
private static long mult(int mult) { //возведение в степень int result = 1; // int - мало! 24^7 = 4_586_471_424L for (int i = 1; i <= mult; i++) result *= 24; return result; }
Java does not control over overfilling, so the method produces an incorrect result, the difference is not equal to zero, the tuition does not stop, the glass ends.
I would have written that:
static long pows[] = new long[8]; static { pows[0] = 1; for ( int i = 1; i < pows.length; i++ ) { pows[i] = pows[i-1]*24; } }
You don't need more than seven for your task,
24^8 = 110_075_314_176
More than37_090_594_994