본문 바로가기

코딩테스트/Coding Bat

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

728x90
728x90

https://codingbat.com/prob/p177372

 

CodingBat Java Warmup-1 max1020

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.max1020(11, 19) → 19max1020(19, 11) → 19max1020(11, 9) → 11Go...Save, Compile, Run (ctrl-enter)Show Solution

codingbat.com



= 문제 번역 =

2개의 양수 int 값이 주어지면 10(포함)..20(포함) 범위에서, 둘 중에 더 큰 값을 반환합니다.

만약 둘 다 해당 범위에 없으면 0을 반환합니다.

 

= 해설 =

< 1 >
public int max1020(int a, int b) {
if((a<10||a>20)&&(b<10||b>20)){
	return 0;
	}else{
  		if(a < b){
    		int temp =a;
    		a=b;
    		b=temp;
 	 		}
  		if (a >= 10 && a <= 20) return a;
  		else return b;
		}
	}
    
< 2 >
public int max1020(int a, int b) {
  // First make it so the bigger value is in a
  if (b > a) {
    int temp = a;
    a = b;
    b = temp;
  }
  
  // Knowing a is bigger, just check a first
  if (a >= 10 && a <= 20) return a;
  if (b >= 10 && b <= 20) return b;
  return 0;
}

728x90