Problem:
Given three ints, a b c, return true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: Math.abs(num) computes the absolute value of a number.
closeFar(1, 2, 10) → true
closeFar(1, 2, 3) → false
closeFar(4, 1, 3) → true
Solution:
public boolean closeFar(int a, int b, int c) { if (Math.abs(a - b) <= 1 && Math.abs(a - c) >= 2 && Math.abs(b - c) >= 2){ return true; } else if (Math.abs(a - c) <= 1 && Math.abs(a - b) >= 2 && Math.abs(b - c) >= 2){ return true; }else{ return false; } }
public boolean closeFar(int a, int b, int c) {
ReplyDeletereturn !close(b,c)&(close(a,b)^close(a,c));
}
public boolean close(int a, int b) {
return Math.abs(a-b)<=1;
}
return Math.abs(a-b)<=1 && Math.abs(a-c)>=2 && Math.abs(b-c)>=2 || Math.abs(a-c)<=1 && Math.abs(a-b)>=2 && Math.abs(b-c)>=2;
ReplyDeletehmmmm
ReplyDeletepublic boolean closeFar(int a, int b, int c) {
ReplyDeleteint x=0,y=0,z=0;
x=Math.abs(a-b);
y=Math.abs(b-c);
z=Math.abs(c-a);
if(a==b||b==c||c==a) return true;
if(x==y||y==z||x==z) return false;
return true;
}
public boolean closeFar(int a, int b, int c) {
ReplyDeleteint diffba = Math.abs(b-a); // equal to 1 or diff by +2
int diffca = Math.abs(a-c); //equal to 1 or diff by +2
int diffbc = Math.abs(b-c); //diff by +2
if (diffbc>=2) {
if (diffba == 1 && diffca>=2){return true;}
else if (diffca == 1 && diffba>=2){return true;}
else if (a==b){return true;}
}
return false;
}
public boolean closeFar(int a, int b, int c) {
ReplyDeleteint diffba = Math.abs(b-a); // equal to 1 or diff by +2
int diffca = Math.abs(a-c); //equal to 1 or diff by +2
int diffbc = Math.abs(b-c); //diff by +2
if (diffbc>=2) {
return (diffba == 1 && diffca>=2) ||(diffca == 1 && diffba>=2)||(a==b);
} return false;
}
public boolean closeFar(int a, int b, int c) {
ReplyDeleteint close1 = Math.abs(a - b);
int close2 = Math.abs(b - c);
int close3 = Math.abs(a - c);
if (close1 <= 1 && close2 >= 2 && close3 >= 2) {
return true;
} else if (close2 <= 1 && close1 >= 2 && close3 >= 2) {
return true;
} else if (close3 <= 1 && close1 >= 2 && close2 >= 2) {
return true;
} else {
return false;
}
}
if (close(a,b) && close(a,c) ){
ReplyDeletereturn false;
}
else if (close(a,b) && far(b,c) ){
return true;
}
else if (close(a,c) && far(b,c) ){
return true;
}
return false;
}
private boolean close (int a, int b){
if (Math.abs(a-b)<=1){
return true;
}
return false;
}
private boolean far (int a, int b){
if (Math.abs(a-b)>=2){
return true;
}
return false;
}
What does the question want us to do? Really confusing
ReplyDeletedont confuse .. its the easiest. if you want answer ,. i can tell you.
Deletepublic boolean closeFar(int a, int b, int c) {
ReplyDeleteint distAB = Math.abs(a - b);
int distBC = Math.abs(b - c);
int distAC = Math.abs(a - c);
if ((distAB <= 1 && distBC <= 1) || (distAB <= 1 && distAC <= 1) || (distBC <= 1 && distAC <= 1)){
return false;
}
return true;
}
boom, boom, boom, boom, I want you in my room!
ReplyDeletepublic boolean closeFar(int a, int b, int c) {
ReplyDeletereturn !(Math.abs(a - b) >= 2 != Math.abs(b - c) >= 2 != Math.abs(c - a) >= 2);
}
def close_far(a, b, c):
ReplyDeletereturn (abs(b - a) < 2) ^ (abs(c - a) < 2) and abs(b - c) > 1
public boolean closeFar(int a, int b, int c)
ReplyDelete{
if (Math.abs(b-a) <= 1) return Math.abs(c-b) >= 2 && Math.abs(c-a) >=
2;
if (Math.abs(c-a) <= 1) return Math.abs(b-c) >= 2 && Math.abs(b-a) >= 2;
return false;
}
public boolean closeFar(int a, int b, int c) {
ReplyDeletereturn Math.abs(a-b)>2 ||Math.abs(a-c)>2 || Math.abs(b-a) <1 || Math.abs(c-a) >2 || Math.abs(b - c) >2 ? true:false;
}
public boolean closeFar(int a, int b, int c) {
ReplyDeleteint diff1 = Math.abs(a-b);
int diff2 = Math.abs(a-c);
int diff3 = Math.abs(b-c);
while(diff3 > 1){
return (diff1<=1&&diff2!=1) || (diff1!=1&&diff2<=1);
}
return false;
}