자료구조 & 알고리즘/알고리즘

[JAVA] 최댓값, 중앙값, 최소값

ReBugs 2022. 12. 29.

Do it! 자료구조와 함께 배우는 알고리즘 입문[자바편] 연습문제와 실습문제입니다.


입력 변수 3개, 최댓값, 중앙값, 최솟값

 

최댓값

static int max(int a, int b, int c)
	{
		int max = a;
		if(b>max)max=b;
		if(c>max)max=c;
		return max;
	}

 

중앙값

static int mid(int a, int b, int c)
	{
		if(a > b)
		{
			if(b > c)return b;
			else if(a > c)return c;
			else return a;
		}
		else if(a>c)return a;
		else if(b>c)return c;
		else return b;
	}

 

최솟값

static int min(int a, int b, int c)
	{
		int min = a;
		if(min>b)min=b;
		if(min>c)min=c;
		return min;
	}

 

종합

import java.util.Scanner;
public class Test {
	static int max(int a, int b, int c)
	{
		int max = a;
		if(b>max)max=b;
		if(c>max)max=c;
		return max;
	}
	static int mid(int a, int b, int c)
	{
		if(a > b)
		{
			if(b > c)return b;
			else if(a > c)return c;
			else return a;
		}
		else if(a>c)return a;
		else if(b>c)return c;
		else return b;
	}
	static int min(int a, int b, int c)
	{
		int min = a;
		if(min>b)min=b;
		if(min>c)min=c;
		return min;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("정수 3개를 입력하세요");
		int []arr = new int[3];
		for(int i = 0; i < 3; ++i)
		{
			arr[i]=sc.nextInt();
		}
		
		System.out.print("최댓값 : "+max(arr[0],arr[1],arr[2])+",");
		System.out.print("중앙값 : "+mid(arr[0],arr[1],arr[2])+",");
		System.out.print("최솟값 : "+min(arr[0],arr[1],arr[2]));
	}
}
/*
정수 3개를 입력하세요
546876 1567544 125
최댓값 : 1567544,중앙값 : 546876,최솟값 : 125
*/

 

 

입력 변수 4개, 최댓값, 최솟값

 

최댓값

static int max(int a, int b, int c, int d)
	{
		int max = a;
		if(max<b) max = b;
		if(max<c) max = c;
		if(max<d) max = d;
		return max;
		
	}

 

최솟값

static int min(int a, int b, int c, int d)
	{
		int min = a;
		if(min>b) min = b;
		if(min>c) min = c;
		if(min>d) min = d;
		return min;
	}

 

종합

import java.util.Scanner;
public class Test{
	static int max(int a, int b, int c, int d)
	{
		int max = a;
		if(max<b) max = b;
		if(max<c) max = c;
		if(max<d) max = d;
		return max;
		
	}
	static int min(int a, int b, int c, int d)
	{
		int min = a;
		if(min>b) min = b;
		if(min>c) min = c;
		if(min>d) min = d;
		return min;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("정수 4개를 입력하세요");
		int []arr = new int[4];
		for(int i = 0; i < 4; ++i)
		{
			arr[i]=sc.nextInt();
		}
		
		System.out.print("최댓값 : "+max(arr[0],arr[1],arr[2],arr[3])+",");
		System.out.print("최솟값 : "+min(arr[0],arr[1],arr[2],arr[3]));
	}
}
/*
정수 4개를 입력하세요
54654 132 654 98321
최댓값 : 98321,최솟값 : 132
*/

 

 

기타

static int mid(int a, int b, int c)
	{
		if(a > b)
		{
			if(b > c)return b;
			else if(a > c)return c;
			else return a;
		}
		else if(a>c)return a;
		else if(b>c)return c;
		else return b;
	}
static int mid(int a, int b, int c)
	{
		if((b>=a && c<=a)||(b<=a && c>=a)) return a;
		else if((a>=b && c<=b)||(a<=b && c>=b)) return b;
		return c;
	}

 

첫 번째 코드와 두 번째 코드는 동일한 기능을 수행한다.

하지만 첫 번째 코드가 더 효율적이다.

두 번째 코드의 (b>=a && c<=a)||(b<=a && c>=a) 에서 b>=a 와 b<= a는 실질적으로 같은 코드이다.

왜냐하면 "a와 b중 둘 중에 뭐가 더 크냐"라고 물어보는 본질은 같기 때문이다.

 또한 다음줄에 나오는 else if((a>=b && c<=b)||(a<=b && c>=b)) 에서도 b>=a 와 b<= a는 실질적으로 같은 코드이다.

같은 물음을 반복적으로 함으로 인해 첫 번째 코드보다 두 번째 코드가 비효율적이다.

댓글