본문으로 바로가기

[코딩테스트]백준문제2920번 음계

category CodingTest 2020. 6. 26. 08:38
728x90
반응형

https://www.acmicpc.net/problem/2920

 

2920번: 음계

다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다. 1부터 8까지 차례대로 연주한다면 ascending, 8

www.acmicpc.net

  1. 오름차순 플래그와 내림차순 플래그를  처음에 true로 선언한다.

 Python 소스

inputList = list(map(int, input().split()))

is_ascending = True
is_descending = True

for i in range(1, len(inputList)):
    firstValue = inputList[i-1]
    secondValue = inputList[i]
    if firstValue > secondValue:
        is_ascending = False
    elif firstValue < secondValue:
        is_descending = False

if is_ascending:
    print("ascending")
elif is_descending:
    print("descending")
else:
    print("mixed")

 PHP 소스

<?php

$input_array = explode(" ", fgets(STDIN));
$is_ascending = true;
$is_descending = true;
for ($i = 1; $i < count($input_array); $i++) {
    $firstValue = $input_array[$i-1];
    $secondValue = $input_array[$i];
    if ($firstValue < $secondValue) {
        $is_descending = false;
    } else if($firstValue > $secondValue) {
        $is_ascending = false;
    }
}

if ($is_ascending) {
    echo "ascending";
} else if ($is_descending) {
    echo "descending";
} else {
    echo "mixed";
}

Java 소스

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

	
	public static void main(String[] args) {
		
		boolean isAscending = true;
		boolean isDecending = true;

		try (Scanner sc = new Scanner(System.in)) {
			String[] splitArray = sc.nextLine().split(" ");
			List<Integer> integerList = Arrays.asList(splitArray).stream().map(a -> Integer.valueOf(a)).collect(Collectors.toList());
			for (int i = 1; i < integerList.size(); i++) {
			Integer firstValue = integerList.get(i-1);
			Integer secondValue = integerList.get(i);
				if (firstValue > secondValue) {
					isAscending = false;
				} else if (firstValue < secondValue) {
					isDecending = false;
				}
			}
			if (isAscending) {
				System.out.print("ascending");
			} else if (isDecending) {
				System.out.print("descending");			
			} else {
				System.out.print("mixed");	
			}
		} catch (Exception ex) {
			System.out.print(ex);
		}

	}

}

 

결과

 

 

 

맞추긴 했으나 결과는 잘 모르겠다ㅠㅠ 더 빠르게 할 방법이 있는 건가.. 다음에 다시 시도하기로...

코딩테스트 너무 어렵당

 

728x90
반응형