Doit/그림으로 쉽게 배우는 자료구조와 알고리즘

세 자리 수 비교하기 _ 트리 알고리즘

prettyNoona 2023. 4. 14. 14:49

 

세자리 수 최대값 구하기

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.println("세 정수의 최대값을 구합니다");
		System.out.println("a의 값: "); int a = sc.nextInt();
		System.out.println("b의 값: "); int b = sc.nextInt();
		System.out.println("c의 값: "); int c = sc.nextInt();
		
		int max = a;
		if(max < b){
			max = b;
		}
		if(max < c){
			max = c;
		}
		
		System.out.println("최대값은 " + max + "입니다");
	}
}

세 정수의 최대값을 구합니다

a의 값:

3

b의 값:

2

c의 값:

1

최대값은 3입니다

 

 

세자리 수 최대값 구하기

package test;

import java.util.Scanner;


public class algorithm_test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Max(3,4,5)" + Max(3,4,5));
	}
	
	static int Max(int a, int b, int c) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("세 정수의 최대값을 구합니다");
		System.out.println("a의 값: "); 
		System.out.println("b의 값: ");
		System.out.println("c의 값: "); 
		
		int max = a;
		if(max < b){
			max = b;
		}
		if(max < c){
			max = c;
		}
		return max;
	}
}​

Max(3,4,5) 5

 

 

세자리 수 중앙값 구하기

package test;

import java.util.Scanner;

public class algorithm_test {
	
	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) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		
		System.out.println("세 정수의 중앙값을 구합니다");
		System.out.println("a의 값: "); int a = sc.nextInt();
		System.out.println("b의 값: "); int b = sc.nextInt();
		System.out.println("c의 값: "); int c = sc.nextInt();
		
		System.out.println("중앙값은 " + med3(a,b,c) + "입니다");
	}​

세 정수의 중앙값을 구합니다

a의 값:

5

b의 값:

3

c의 값:

1

중앙값은 3입니다