Algorithm/기타(기업등)
[CodingBat/java] luckySum
IagreeBUT
2021. 10. 7. 02:58
728x90
링크
https://codingbat.com/prob/p130788
CodingBat Java Logic-2 luckySum
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.luckySum(1, 2, 3) → 6luckySum(1, 2, 1
codingbat.com
문제
13이 나오면, (13포함) 그 이후의 수는 연산에 포함시키지 않는다.
풀이
순서대로 13인지 검사해서 return
코드
public int luckySum(int a, int b, int c) {
if(a == 13) return 0;
if(b == 13) return a;
if(c == 13) return a+b;
return a+b+c;
}
728x90