Doit! 알고리즘 (1)

업데이트:


chap01-01

static int max4(int a, int b, int c, int d)

package chap01;
import java.util.Scanner;

public class Q1 {
	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) {
		Scanner sc = new Scanner(System.in);
		int a, b, c, d = 0;
		
		System.out.println("네 정수 입력");
		System.out.print("a의 값 : ");
		a = sc.nextInt();
		System.out.print("b의 값 : ");
		b = sc.nextInt();
		System.out.print("c의 값 : ");
		c = sc.nextInt();
		System.out.print("d의 값 : ");
		d = sc.nextInt();
		
		System.out.print("최댓값 : " + max4(a,b,c,d));
	}
}



chap01-02

static int min3(int a, int b, int c)

package chap01;
import java.util.Scanner;

public class Q2 {
	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[] agrs) {
		Scanner sc = new Scanner(System.in);
		int a, b, c = 0;
		
		System.out.println("세 정수 입력");
		System.out.print("a의 값 : ");
		a = sc.nextInt();
		System.out.print("b의 값 : ");
		b = sc.nextInt();
		System.out.print("c의 값 : ");
		c = sc.nextInt();

		System.out.print("최솟값 : " + min3(a,b,c));
	}
}



chap01-03

static int min4(int a, int b, int c, int d)

package chap01;
import java.util.Scanner;

public class Q3 {
	static int min4(int a, int b, int c, int d) {
		int min = a;
		
		if(b < min) min = b;
		if(c < min) min = c;
		if(d < min) min = d;
		
		return min;
	}
	public static void main(String[] agrs) {
		Scanner sc = new Scanner(System.in);
		int a, b, c, d = 0;
		
		System.out.println("네 정수 입력");
		System.out.print("a의 값 : ");
		a = sc.nextInt();
		System.out.print("b의 값 : ");
		b = sc.nextInt();
		System.out.print("c의 값 : ");
		c = sc.nextInt();
		System.out.print("d의 값 : ");
		d = sc.nextInt();
		
		System.out.print("최솟값 : " + min4(a,b,c,d));
	}
}



chap01-04

static int med3(int a, int b, int c)

package chap01;
public class Q4 {
	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) {
		System.out.println("med3(3,2,1) = " + med3(3, 2, 1)); // a>b>c
		System.out.println("med3(3,2,2) = " + med3(3, 2, 2)); // a>b=c
		System.out.println("med3(3,1,2) = " + med3(3, 1, 2)); // a>c>b
		System.out.println("med3(3,2,3) = " + med3(3, 2, 3)); // a=c>b
		System.out.println("med3(2,1,3) = " + med3(2, 1, 3)); // c>a>b
		System.out.println("med3(3,3,2) = " + med3(3, 3, 2)); // a=b>c
		System.out.println("med3(3,3,3) = " + med3(3, 3, 3)); // a=b=c
		System.out.println("med3(2,2,3) = " + med3(2, 2, 3)); // c>a=b
		System.out.println("med3(2,3,1) = " + med3(2, 3, 1)); // b>a>c
		System.out.println("med3(2,3,2) = " + med3(2, 3, 2)); // b>a=c
		System.out.println("med3(1,3,2) = " + med3(1, 3, 2)); // b>c>a
		System.out.println("med3(2,3,3) = " + med3(2, 3, 3)); // b=c>a
		System.out.println("med3(1,2,3) = " + med3(1, 2, 3)); // c>b>a
	}
}



chap01-05

static int med3(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;
}

가장 처음의 if가 성립한 경우에도 2번째 if 문이 수행되므로 효율이 나빠진다.

태그:

카테고리:

업데이트:

댓글남기기