Problem:
Return true if the given non-negative number is 1 or 2 less than a multiple of 20. So for example 38 and 39 return true, but 40 returns false. See also: Introduction to Mod
less20(18) → true
less20(19) → true
less20(20) → false
Solution:
public boolean less20(int n) { return (n + 1) % 20 == 0 || (n + 2) % 20 == 0; }
public boolean more20(int n) {
ReplyDeletereturn (n % 20 == 1) != (n % 20 == 2);
}
public boolean less20(int n) {
ReplyDeletereturn (n%20 == 18 || n%20 == 19);
}
return (n+1)%20==0||(n+2)%20==0;
ReplyDelete