Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0. Note: the % "mod" operator computes the remainder, e.g. 7 % 5 is 2.

maxMod5(2, 3) → 3
maxMod5(6, 2) → 6
maxMod5(3, 2) → 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public int maxMod5(int a, int b) {
  int smaller=a>=b?b:a;
  int larger=a>=b?a:b;
  if (a==b) {
    return 0;
  } else if (a%5==b%5) {
    return smaller;
  } else {
    return larger;
  }
}

Keresés

Ajánló