본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] Warmup - 1단계 : hasTeen 문제

728x90
728x90

https://codingbat.com/prob/p178986

 

CodingBat Java Warmup-1 hasTeen

We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 3 int values, return true if 1 or more of them are teen.hasTeen(13, 20, 10) → truehasTeen(20, 19, 10) → truehasTeen(20, 10, 13) → trueGo...Save, Compile, Run (ctrl-enter)

codingbat.com



= 문제 번역 =

숫자가 13..19(포함) 범위에 있으면 숫자가 "10대"라고 합니다. 

3개의 int 값이 주어지면 그 중 1개 이상이 10대이면 true를 반환합니다.

 

= 문제푸는 팁 =

return 자체에 조건을 걸면, 그 조건 자체가 해당하면 true가 나오는 부분을 상기하기!

 

 

= 해설 =

< 1 >
public boolean hasTeen(int a, int b, int c) {
  if((a>=13&&a<=19)||(b>=13&&b<=19)||(c>=13&&c<=19)) return true;
  else return false;
}

< 2 >
public boolean hasTeen(int a, int b, int c) {
  return (a>=13 && a<=19) ||
         (b>=13 && b<=19) ||
         (c>=13 && c<=19);
}

728x90