Java > Logic-1 > blueTicket (CodingBat Solution)

Problem:

You have a blue lottery ticket, with ints a, b, and c on it. This makes three pairs, which we'll call ab, bc, and ac. Consider the sum of the numbers in each pair. If any pair sums to exactly 10, the result is 10. Otherwise if the ab sum is exactly 10 more than either bc or ac sums, the result is 5. Otherwise the result is 0.

blueTicket(9, 1, 0) → 10
blueTicket(9, 2, 0) → 0
blueTicket(6, 1, 4) → 10


Solution:

public int blueTicket(int a, int b, int c) {
  int ab = a + b;
  int bc = b + c;
  int ac = a + c;
  
  if (ab == 10 || bc == 10 || ac == 10)
    return 10;
  if (ab - bc == 10 || ab - ac == 10)
    return 5;
  else
    return 0;
}


4 comments :

  1. public int blueTicket(int a, int b, int c) {
    int ab = a + b;
    int bc = b + c;
    int ac = a + c;
    int abbc = ab - bc;
    int abac = ab - ac;

    return ab == 10 || bc == 10 || ac == 10 ? 10 : abbc == 10 || abac == 10? 5 : 0;
    }

    ReplyDelete
  2. public int blueTicket(int a, int b, int c) {
    return (a + b == 10 || b + c == 10 || c + a == 10)? 10 : (a == c + 10 || b == c + 10)? 5 : 0;
    }

    ReplyDelete
  3. public int blueTicket(int a, int b, int c) {
    if (a + b == 10 || b + c == 10 || c + a == 10){
    return 10;
    }
    if(a == c + 10 || b == c + 10){
    return 5;
    }
    return 0;
    }

    ReplyDelete
  4. public int blueTicket(int a, int b, int c) {
    return a+b==10 || b+c==10 || c+a==10 ? 10 : (a==b+10 || a==b-10) ? 5 : (b==c+10 || b==c-10) ? 5 : a==c+10 || a==c-10 ? 5:0 ;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact