Answer the question
In order to leave comments, you need to log in
What's wrong with the indexOf method?
The code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
while (!(1 <= a && a <= b && b <= Math.pow(10, 6) && b - a <= 3000)) {
System.out.println("Вы ввели неккоректное значение");
a = in.nextInt();
b = in.nextInt();
}
int[] masOfNumbers = new int[b-a+1];
int[] masOfDividers = new int[b-a+1];
for(int d = 1; d<=(b-a+1); d++){
masOfNumbers[d-1] = a+d-1;
}
for(int c = 1; c<=(b-a+1); c++){
masOfDividers[c-1] = 0;
}
for(int i = a; i<=b; i++){
for(int j = 1; j<=i; j++){
if(i%j==0){
masOfDividers[i-a]++;
}
}
}
int max = getMax(masOfDividers);
String str = Arrays.toString(masOfDividers).replaceAll("\\[|\\]|,|\\s", "");
System.out.println(masOfNumbers[str.indexOf(max)]);
}
public static int getMax(int[] arrayForFindingTheMaximum)
{
int maximum = Integer.MIN_VALUE;
for (int i = 0; i < arrayForFindingTheMaximum.length; i++)
{
maximum = Math.max(maximum, arrayForFindingTheMaximum[i]);
}
return maximum;
}
}
Answer the question
In order to leave comments, you need to log in
you should use debug to understand how it works
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
public static void main(String[] args) {
String str = "1223242434262445262644283";
System.out.println(str.indexOf(50)); //1, потому что '2' == 50
}
public static void main(String[] args) {
String str = "1223242434262445262644283";
System.out.println(str.indexOf("23")); //2 - здесь работает другая реализация метода indexOf
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question