728x90
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int[] scale = {8,7,6,5,4,3,2,1};
String[] resultText = {"ascending", "descending", "mixed"};
int index;
int[] input = new int[scale.length];
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str);
for (int i = 0; i < scale.length; i++) {
input[i] = Integer.parseInt(st.nextToken());
}
if (Arrays.equals(scale, input)) {
index = 1;
} else {
Arrays.sort(scale);
if (Arrays.equals(scale, input)) {
index = 0;
} else {
index = 2;
}
}
bw.write(resultText[index]);
bw.flush();
bw.close();
}
}
○ 문제
다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다.
1부터 8까지 차례대로 연주한다면 ascending, 8부터 1까지 차례대로 연주한다면 descending, 둘 다 아니라면 mixed 이다.
연주한 순서가 주어졌을 때, 이것이 ascending인지, descending인지, 아니면 mixed인지 판별하는 프로그램을 작성하시오.
○ 입력
첫째 줄에 8개 숫자가 주어진다. 이 숫자는 문제 설명에서 설명한 음이며, 1부터 8까지 숫자가 한 번씩 등장한다.
○ 출력
첫째 줄에 ascending, descending, mixed 중 하나를 출력한다.
○ 예제 입력
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
8 1 7 2 6 3 5 4
○ 예제 출력
ascending
descending
mixed
○ 확인
- 배열 비교 Arrays.equals(arr1, arr2)
- 내림차순 구현하기 귀찮으니 기준 데이터를 역정렬하여 비교하였음
728x90
'○ 기술면접 > 알고리즘' 카테고리의 다른 글
| 구현: 단어공부 (백준 1157) (0) | 2023.03.23 |
|---|---|
| 사칙연산: 평균 (백준 1546) (0) | 2023.03.23 |
| 구현: 검증수 (백준 2475) (0) | 2023.03.23 |
| 구현: 상수 (백준 2908) (0) | 2023.03.23 |
| [알고리즘] 정렬: 연속된 데이터를 기준에 따라 정렬 (0) | 2023.03.22 |
| DFS: 섬의 개수 (백준 4963) (0) | 2023.03.22 |
| 구현: 단지번호붙이기 (백준 2667) (0) | 2023.03.22 |
| DFS: 바이러스 (백준 2606) (1) | 2023.03.22 |