일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- pop
- ADSL
- first view
- Array
- javascript
- 객체
- 자바스크립트 함수
- bom
- 레거시 마이그레이션
- 반응형 디자인
- release
- 직귀율
- 인공지능
- 내장객체
- 不不怕变
- 릴리스
- 중요한건 꺾이지 않는 마음
- 기술면접 후기
- 제어문
- 사이트 이동 경로
- Done is better than perfect
- Great things take time
- 자바스크립트
- 각자의 밤
- 퍼스트 뷰
- Electronic Commerece
- 배열
- 도메인
- 怕不变
- javascript function
- Today
- Total
1일1끄적
자료구조&기본 알고리즘 입문(자바)- 알고리즘? 본문
● 세 정수의 최대값을 구하기
import java.util.Scanner;
// 3개의 정숫값을 입력하고 최댓값 구하기
class Max3 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("세 정수의 최댓값 입력");
System.out.print("a의 값:"); int a = stdIn.nextInt();
System.out.print("b의 값:"); int b = stdIn.nextInt();
System.out.print("c의 값:"); int c = stdIn.nextInt();
int max = a;
if (b > max) max = b;
if (c > max) max = c;
System.out.println("최댓값은 " + max");
}
}
최대값을 구하는 과정은 다음과 같다.
① max에 a 값을 넣는다
② b 값이 max보다 크면 max에 b 값을 넣는다.
③ c 값이 max보다 크면 max에 c값을 넣는다.
이렇게 여러 문장(프로세스)가 순차적으로 실행되는 구조를 순차적(concatenation)구조라고 한다.
① 은 단순한 대입, ② /③은 if 문. ( ) 안에 있는 식의 평가 결과에 따라 프로그램의 실행 흐름을
변경하는 if문을 선택(selection)구조라고 한다.
// 3개의 정숫값 가운데 최댓값을 구하여 출력.
class Max3m {
// a, b, c의 최댓값을 구하여 반환.
static int max3(int a, int b, int c) {
int max = a; // 최댓값
if (b > max)
max = b;
if (c > max)
max = c;
return max;
}
// 1,2,3 을 입력했을 떄의 경우의 수
public static void main(String[] args) {
System.out.println("max3(3,2,1) = " + max3(3, 2, 1)); // [A] a>b>c
System.out.println("max3(3,2,2) = " + max3(3, 2, 2)); // [B] a>b=c
System.out.println("max3(3,1,2) = " + max3(3, 1, 2)); // [C] a>c>b
System.out.println("max3(3,2,3) = " + max3(3, 2, 3)); // [D] a=c>b
System.out.println("max3(2,1,3) = " + max3(2, 1, 3)); // [E] c>a>b
System.out.println("max3(3,3,2) = " + max3(3, 3, 2)); // [F] a=b>c
System.out.println("max3(3,3,3) = " + max3(3, 3, 3)); // [G] a=b=c
System.out.println("max3(2,2,3) = " + max3(2, 2, 3)); // [H] c>a=b
System.out.println("max3(2,3,1) = " + max3(2, 3, 1)); // [I] b>a>c
System.out.println("max3(2,3,2) = " + max3(2, 3, 2)); // [J] b>a=c
System.out.println("max3(1,3,2) = " + max3(1, 3, 2)); // [K] b>c>a
System.out.println("max3(2,3,3) = " + max3(2, 3, 3)); // [L] b=c>a
System.out.println("max3(1,2,3) = " + max3(1, 2, 3)); // [M] c>b>a
}
}
위의 예제는 int형 매개변수 a, b, c, 의 값을 받아 최댓값을 구하고 그것을 int형 값으로 반환하는 메서드.
● 연습문제
-네 값의 최댓값을 구하는 max4 메서드
// 네 값의 최댓값을 구하는 max4메서드
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) {
System.out.println(max4(7,10,11,15));
System.out.println(max4(20,10,11,15));
System.out.println(max4(20,30,11,15));
System.out.println(max4(20,30,41,15));
System.out.println(max4(20,30,41,55));
}
}
-세 값의 최솟값을 구하는 min3 메서드
// 세 값의 최솟값을 구하는 min3 메서드 작성
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[] args) {
System.out.println(min3(10,15,20));
System.out.println(min3(30,15,20));
System.out.println(min3(30,40,20));
}
}
-네 값의 최솟값을 구하는 min4 메서드
// 네 값의 최솟값을 구하는 min4 메서드
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[] args) {
System.out.println(min4(1,2,3,4));
System.out.println(min4(3,2,4,5));
System.out.println(min4(4,5,3,6));
System.out.println(min4(5,6,7,4));
}
}
앞선 경우와 같이 세값의 대소 관계의 조합은 13가지 종류가 있는데, 이런 조합을 나열한 모양이
나무(tree)형태 이므로 결정트리(descision tree)라고 한다.
결정 트리는 왼쪽 끝에서 시작하여 오른쪽으로 이동한다.
( ) 안의 조건이 성립하면 윗가지로,성립하지 않으면 아랫가지로 이동한다.
● 세 값의 중앙값
최댓값, 최소값과 달리 중앙값을 구하는 절차는 복잡해서 수많은 알고리즘이 있다고 한다.
import java.util.Scanner;
// 3개의 정숫값을 입력하고 중앙값을 구한 다음 출력.
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 stdIn = new Scanner(System.in);
System.out.println("세 정수의 중앙값 구하기.");
System.out.print("a의 값:");
int a = stdIn.nextInt();
System.out.print("b의 값:");
int b = stdIn.nextInt();
System.out.print("c의 값:");
int c = stdIn.nextInt();
System.out.println("중앙값은 " + med3(a, b, c));
}
}
● 조건 판단과 분기
정수값의 부호(양수/음수/0)을 판단하여 출력하는 프로그램
import java.util.Scanner;
// 입력한 정숫값이 양수인지 음수인지 0인지 판단.
class JudgeSign {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("정수를 입력.:");
int n = stdIn.nextInt();
if (n > 0)
System.out.println("이 수는 양수.");
else if (n < 0)
System.out.println("이 수는 음수.");
else
System.out.println("이 수는 0.");
}
}
변수 n의 값이 양수면 " 이수는 양수", 음수면 " 이수는 음수", 0이면 "이 수는 0" 이 실행..
실행되는 부분은 위의 3가지 중 한가지 뿐이며 두 가지가 동시에 실행되거나 하나도 실행되지 않거나
하는 경우는 없다..
if (n == 1)
System.out.println("이 수는 1입니다.");
else if (n == 2)
System.out.println("이 수는 2입니다.");
else if (n == 3)
System.out.println("이 수는 3입니다.");
출력되는 부분만 수정하여 n의 값이 1이면 "이 수는 1입니다", 2이면 " 이 수는 2입니다", 3이면 "이 수는 3입니다" 실행
-출처: 자료구조와 함꼐 배우는 알고리즘 입문[자바편] 책 중
http://www.yes24.com/Product/Goods/60547893
'개발 > Java' 카테고리의 다른 글
자료구조&기본 알고리즘 입문(자바)- 배열(4) (0) | 2022.01.20 |
---|---|
자료구조&기본 알고리즘 입문(자바)- 배열(3) (0) | 2022.01.03 |
자료구조&기본 알고리즘 입문(자바)- 배열(2) (0) | 2021.12.26 |
자료구조&기본 알고리즘 입문(자바)- 배열(1) (0) | 2021.12.22 |
자료구조&기본 알고리즘 입문(자바)- 반복 (0) | 2021.12.18 |