You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
780 B
Java
32 lines
780 B
Java
import java.util.function.Predicate;
|
|
|
|
public class Calculations {
|
|
public static void main(String[] args) {
|
|
char[] chars = {'a', 'b', 'c', 'd'};
|
|
// поиск bba
|
|
calculate(chars, 3, i -> i[0] == 1 && i[1] == 1 && i[2] == 0);
|
|
}
|
|
static void calculate(char[] a, int k, Predicate<int[]> decider) {
|
|
int n = a.length;
|
|
if (k < 1 || k > n)
|
|
throw new IllegalArgumentException("Forbidden");
|
|
int[] indexes = new int[n];
|
|
int total = (int) Math.pow(n, k);
|
|
while (total-- > 0) {
|
|
for (int i = 0; i < n - (n - k) ; i++)
|
|
System.out.print(a[indexes[i]]);
|
|
System.out.println();
|
|
if (decider.test(indexes) )
|
|
break;
|
|
for (int i=0; i < n; i++) {
|
|
if (indexes[i] >= n - 1) {
|
|
indexes[i] = 0;
|
|
} else {
|
|
indexes[i]++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|