티스토리 뷰

 

문제는 위와 같으며 ascending, desceding 변수를 선언하고 주어진 음계 배열에서 앞 음계가 뒤 음계보다 작으면(오름차순) descending 을 false 로 크면(내림차순) ascending 을 false 로 만듭니다. 결과 출력 시 ascending, descending 중 true 인 값을 출력하고 둘다 false 인 경우 mixed 를 출력하도록 구현하였습니다.

 

파이썬 코드를 보면 다음과 같습니다.

from sys import stdin

inputs = list(map(int, stdin.readline().split()))

ascending = True
descending = True

for i in range(1, 8):
    if inputs[i] > inputs[i - 1]:
        descending = False
    elif inputs[i] < inputs[i - 1]:
        ascending = False

if ascending:
    print('ascending')
elif descending:
    print('descending')
else:
    print('mixed')

 

자바 코드는 다음과 같습니다.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] tmp = br.readLine().split(" ");

        boolean ascending = true;
        boolean descending = true;

        for(int i = 1; i < tmp.length; i++) {
            if(Integer.parseInt(tmp[i]) > Integer.parseInt(tmp[i - 1])) {
                descending = false;
            } else if(Integer.parseInt(tmp[i]) < Integer.parseInt(tmp[i - 1])) {
                ascending = false;
            }
        }

        if (ascending) {
            System.out.println("ascending");
        } else if (descending) {
            System.out.println("descending");
        } else {
            System.out.println("mixed");
        }
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 31
글 보관함