JAVA/JAVA Algorithm, Datastruct

[JAVA]알고리즘 입문 (최솟값 최댓값)

큐범 2021. 7. 21. 17:38

Do it! 알고리즘 입문 [자바편]

알고리즘 이란?

문제를 해결하기 위한 것으로, 명확하게 정의되고 순서가 있는 유한 개의 규칙으로 이루어진 집합

 

세 값의 최댓값 구하는 예제

package chap01;

import java.util.Scanner;

public class Max3 {

	public static void main(String[] args) {
		Scanner stdId = new Scanner(System.in);
		
		System.out.println("세 정수의 최댓값을 구합니다.");
		System.out.print("a의 값 : "); int a = stdId.nextInt();
		System.out.print("b의 값 : "); int b = stdId.nextInt();
		System.out.print("c의 값 : "); int c = stdId.nextInt();
		int max = a;
		if(b > max) max = b;
		if(c > max) max = c;
		
		System.out.println("최댓값은 "+max+"입니다.");	
	}
}
//결과
세 정수의 최댓값을 구합니다.
a의 값 : 20
b의 값 : 30
c의 값 : 13
최댓값은 30입니다.
package chap01;

public class Max3m {
	static int max3(int a, int b, int c) {
		int max = a;
		if(b > max)
			max = b;
		if(c > max)
			max = c;
		
		return max;
	}
	
	public static void main(String[] args) {
		System.out.println("max3(3,2,1) = " + max3(3,2,1));
		System.out.println("max3(3,2,2) = " + max3(3,2,2));
		System.out.println("max3(3,1,2) = " + max3(3,1,2));
		System.out.println("max3(3,2,3) = " + max3(3,2,3));
		System.out.println("max3(2,1,3) = " + max3(2,1,3));
		System.out.println("max3(3,3,2) = " + max3(3,3,2));
		System.out.println("max3(3,3,3) = " + max3(3,3,3));
		System.out.println("max3(2,2,3) = " + max3(2,2,3));
		System.out.println("max3(2,3,1) = " + max3(2,3,1));
		System.out.println("max3(2,3,2) = " + max3(2,3,2));
		System.out.println("max3(1,3,2) = " + max3(1,3,2));
		System.out.println("max3(2,3,3) = " + max3(2,3,3));
		System.out.println("max3(1,2,3) = " + max3(1,2,3));		
	}
}

매개변수 - 메서드를 정의 할 때 메서드에 전달되는 값을 저장하기 위해 변수를 선언하는 것을 매개변수 또는 형식매개변수라 정의한다. 즉, 메서드를 정의할 때는 '매개변수' 메서드를 호출 할 때는 '실인수'라 한다.

위에 예제는 main메서드에 max3 메서드에 세 값을 실인수로 주어 호출하고 반환값을 화면에 13회 출력합니다.

네 값의 최댓값을 구하는 알고리즘

package chap01;

public class Max4 {
	
	static int max4(int a, int b, int c, int d) {
		int max = a;
		if(b > max)
			max = b;
		if(c > max)
			max = c;
		if(d > max)
			max = d;
		return max;
	}
	
	public static void main (String[] args) {
		System.out.println("max(6, 10, 100, 90) = " + max4(6,10,100,90));
	}
}

세 값의 최솟값을 구하는 알고리즘

package chap01;

public class Min3 {

	static int min3(int a, int b, int c) {
		int min = a;
		if(b < min)
			min = b;
		if(c < min)
			min = c;
		return min;
	}
	
	public static void main(String[] args) {
		System.out.println("max3(3,2,1) = " + min3(3,2,1));
	}

}

네 값의 최솟값을 구하는 알고리즘

package chap01;

public class Max4 {
	
	static int max4(int a, int b, int c, int d) {
		int max = a;
		if(b > max)
			max = b;
		if(c > max)
			max = c;
		if(d > max)
			max = d;
		return max;
	}
	
	public static void main (String[] args) {
		System.out.println("max(6, 10, 100, 90) = " + max4(6,10,100,90));
	}
}

세번의 중앙 값

package chap01;

import java.util.Scanner;

public class Median {
 
	static int med3(int a, int b, int c) {
		if(a >= b)
			if(b >= c)
				return b;
			else if (a <= c)
				return a;
			else
				return c;
		else if (a>c)
			return a;
		else if (b>c)
			return c;
		else
			return b;
	}
	
	public static void main(String[] args) {
		Scanner stdId = new Scanner(System.in);
		
		System.out.println("세 정수의 중앙값을 구합니다.");
		System.out.print("a의 값 : ");
		int a = stdId.nextInt();
		System.out.print("b의 값 : ");
		int b = stdId.nextInt();
		System.out.print("c의 값 : ");
		int c = stdId.nextInt();
		
		System.out.println("중앙값은 "+med3(a,b,c)+"입니다.");
	}
 
}

조건 판단과 분기

입력한 값이 음수, 양수, 0인지 판단하는 알고리즘

package chap01;

import java.util.Scanner;

public class JudgeSign {

	public static void main(String[] args) {
		Scanner stdId = new Scanner(System.in);
		
		System.out.print("정수를 입력하세요 : ");
		int n = stdId.nextInt();
		
		if(n > 0)
			System.out.println("입력 한 값은 양수입니다.");
		else if(n < 0)
			System.out.println("입력 한 값은 음수 입니다.");
		else
			System.out.println("이 수는 0입니다.");
		
	}

}

if문에서 정수는 양수, 음수, 0에 속하기에 그 중에서 해당되지 않는 경우는 존재하지 않는다. 따라서, if문에서 두 가지가 실행되거나 하나도 실행되지 않는 경우는 존재하지 않는다.