JAVA. Перебор комбинаций через else if. Нужно уменьшить код
есть константа; пользователь вводит 5 значений; если какое то(какие то) значения меньше константы, то алгоритм присваивает и возвращает уникальное число. С объяснениями, всегда было плохо((( прощу прощения(((
public int Block(int push1,int pull1, int sq1, int abc1, int sp1 )
{
int res;
int push = 30;
if (push1< push) {
pushConst=0;
}
int pull = 12;
if (pull1< pull){
pullConst=0;
}
int squat = 60;
if (sq1< sq){
sqConst=0;
}
int abc = 50;
if (abc1< abc){
abConst=0;
}
int sp = 50;
if (sp1< sp){
spConst =0;
}
if (pushConst==1&&pullConst==1&&sqConst==1&&abConst==1&& spConst ==1) {
res = 1;
} else if (pushConst==1&&pullConst==1&&sqConst==1&&abcConst==1) {
res = 2;
} else if (pushConst==1&&sqConst==1&&abConst==1&& spConst ==1){
res=3;
} else if (pushConst==1&&pullConst==1&&abConst==1&& spConst ==1){
res=4;
} else if (pushConst==1&&pullConst==1&&sqConst==1&& spConst ==1){
res=5;
} else if (pullConst==1&&sqConst==1&&abConst==1&& spConst ==1){
res=6;
} else if (pushConst==1&&pullConst==1&&sqConst==1){
res=7;
} else if (pushConst==1&&sqConst==1&&abConst==1){
res=8;
} else if (pushConst==1&&pullConst==1&&abConst==1){
res=9;
} else if (pushConst==1&&pullConst==1&& spConst ==1){
res=10;
} else if (pullConst==1&&sqConst==1&&abConst==1) {
res =11;
} else if (pullConst==1&&abConst==1&& spConst ==1) {
res=12;
} else if (squatConst==1&&abConst==1&& spConst ==1){
res = 13;
} else if (pushConst==1&&sqConst==1&& spConst ==1){
res = 14;
} else if (pullConst==1&&sqConst==1&& spConst ==1){
res=15;
} else if (pushConst==1&&abConst==1&& spConst ==1) {
res = 16;
} else if (pushConst==1&&pullConst==1) {
res = 17;
} else if (pushConst==1&&sqConst==1) {
res = 18;
}else if (pushConst==1&&abConst==1) {
res = 19;
} else if (pushConst==1&& spConst ==1) {
res = 20;
} else if (pullConst==1&&sqConst==1) {
res = 21;
} else if (pullConst==1&&abConst==1) {
res = 22;
} else if (pullConst==1&& spConst ==1) {
res = 23;
} else if (sqConst==1&&abConst==1) {
res = 24;
} else if (sqConst==1&& spConst ==1) {
res = 25;
} else if (abConst==1&& spConst ==1) {
res = 26;
} else if (pushConst==1) {
res = 27;
}else if (pullConst==1) {
res = 28;
}else if (sqConst==1) {
res = 29;
}else if (abConst==1) {
res = 30;
}else if (spConst ==1) {
res = 31;
} else {
res = 32;
}
return res;
Ответы (1 шт):
Автор решения: Nowhere Man
→ Ссылка
Как было отмечено в комментариях Владимиром Клыковым, входные значения push1, pull1, sq1, ab1, sp1 переводятся в битовые флаги xxxConst, которые соответственно можно использовать для вычисления индекса в массиве, значения которого будут соответствовать нужным значениям res:
push pull sq ab sp index res
1 1 1 1 1 31 1
1 1 1 1 - 30 2
...
1 - - - - 16 27
- 1 - - - 8 28
- - 1 - - 4 29
- - - 1 - 2 30
- - - - 1 1 31
- - - - - 0 32
Таким образом можно будет полностью избавиться от операторов if.
public static int blockNew(int push1, int pull1, int sq1, int abc1, int sp1)
{
int push = push1 < 30 ? 0 : 16;
int pull = pull1 < 12 ? 0 : 8;
int sq = sq1 < 60 ? 0 : 4;
int ab = abc1 < 50 ? 0 : 2;
int sp = sp1 < 50 ? 0 : 1;
final int[] arr = {
32, 31, 30, 26, 29, 25, 24, 13, // 0 ... 7
28, 23, 22, 12, 21, 15, 11, 6, // 8 ... 15
27, 20, 19, 16, 18, 14, 8, 3, // 16 ... 23
17, 10, 9, 4, 7, 5, 2, 1 // 24 ... 31
};
return arr[push + pull + sq + ab + sp];
}
Элементарный тест для сравнения:
int ok = 0;
int f = 0;
for (int push: new int[]{1, 30}) {
for (int pull: new int[]{1, 12}) {
for (int sq: new int[]{1, 60}) {
for (int ab: new int[]{1, 50}) {
for (int sp: new int[]{1, 50}) {
int r1 = Block(push, pull, sq, ab, sp);
int r2 = blockNew(push, pull, sq, ab, sp);
if (r1 == r2) {
ok++;
} else {
f++;
System.out.println("Diff: old=" + r1 + "; new=" + r2);
}
}
}
}
}
}
System.out.println("Ok=" + ok + "; fails=" + f);
// -> Ok=32; fails=0