Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Реализуйте метод, возвращающий true, если среди четырех его аргументов ровно два истинны #1

Open
andrei-sapeshka opened this issue Nov 6, 2016 · 6 comments

Comments

@andrei-sapeshka
Copy link

andrei-sapeshka commented Nov 6, 2016

Может?

public static boolean booleanExpression(boolean a, boolean b, boolean c, boolean d) {
    return ((a ^ b) & (c ^ d)) | ((a ^ c) & (b ^ d)) | ((a ^ d) & (b ^ c));
}
@demidovakatya
Copy link
Owner

кажется, у меня потом так в итоге и было 🤔

@andrei-sapeshka
Copy link
Author

Нет, у Вас было так:

public static boolean booleanExpression(boolean a, boolean b, boolean c, boolean d) {
    return (a & b & !c & !d) | (a & !b & c & !d) | (a & !b & !c & d) | (!a & b & c & !d) | (!a & b & !c & d) | (!a & !b & c & d);
}

Я же предлагаю:

public static boolean booleanExpression(boolean a, boolean b, boolean c, boolean d) {
    return ((a ^ b) & (c ^ d)) | ((a ^ c) & (b ^ d)) | ((a ^ d) & (b ^ c));
}

@demidovakatya
Copy link
Owner

окей, спасибо, попробую :)

@Nikasan
Copy link

Nikasan commented Sep 30, 2018

public static boolean booleanExpression(boolean a, boolean b, boolean c, boolean d) {
return ((a ^ b) && (c ^ d)) || ((a ^ c) && (b ^ d)) || ((a ^ d) && (b ^ c));
}
Better for performance using && and ||

@Dmitriy-Kuznetsov1993
Copy link

@andrei-sapeshko, спс друг, ты очень мне помог!

@oleksandr-kravchenko
Copy link

return ((a ^ b) && (c ^ d)) || ((a ^ c) && (b ^ d));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants