computer_study

[배열] BAEKJOON 2920번 '음계'문제 본문

알고리즘/문제풀이

[배열] BAEKJOON 2920번 '음계'문제

knowable 2020. 7. 25. 21:43

문제 : www.acmicpc.net/problem/2920

 

2920번: 음계

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

www.acmicpc.net

 

  • 파이썬 코드
arr = list(map(int, input().split(' ')))  # python에서 입력받기

ascending = True
descending = True

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

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

 

 

  • c++ 코드
#include <iostream>
using namespace std;

enum resul{
    ascending,
    descending,
    mixed
};

int main(){
    
    int arr[8]={};
    int check = mixed;

    for(int i = 0 ; i< 8 ; i++){
        cin >> arr[i];
    }
    
    for(int i = 0 ; i < 8 ; i++){
        
        if(i == 0){
            if(arr[i]==1)
                check = ascending;
            else if(arr[i]==8)
                check = descending;
            else{
                check = mixed;
                break;
            }
        }
        
        else{
            if(check == ascending && arr[i] == i+1){
                check = ascending;
            }
            else if(check == descending && arr[i] == 8-i){
                check = descending;
            }
            else
                check = mixed;
        }
    }
    if(check == ascending)
        cout << "ascending" <<endl;
    else if(check == descending)
        cout << "descending" <<endl;
    else
        cout << "mixed" <<endl;

    return 0;
}

 

Comments