본문 바로가기
Algorithm/Baekjoon

[백준/BOJ] 2920번 - 음계 (C++)

by shine-jung 2021. 7. 5.
반응형

문제 링크

 

코딩하기 전 생각하기

/*
8개의 숫자를 입력받아 벡터에 저장한다.
조건문으로 벡터을 판별하여 ascending, descending, mixed 중 하나를 출력한다.
	벡터가 1 2 3 4 5 6 7 8 이라면 “ascending”을 출력한다.
	벡터가 8 7 6 5 4 3 2 1 이라면 “descending”을 출력한다.
	둘 다 아니라면 “mixed”를 출력한다.
*/

 


 

코드

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    vector<int> arr(8);
    vector<int> ascending  = {1, 2, 3, 4, 5, 6, 7, 8};
    vector<int> descending = {8, 7, 6, 5, 4, 3, 2, 1};
    
    for ( int i = 0; i < 8; i++ ) {
        cin >> arr[i];
    }
    if ( arr == ascending ) {
        cout << "ascending" << endl;
    }
    else if ( arr == descending ) {
        cout << "descending" << endl;
    }
    else {
        cout << "mixed" << endl;
    }
}

 


 

느낀점

 

is_sorted 함수와 같은 알고리즘을 구현해야될 것 같기도 한데.. 풀 문제는 많고 시간은 없으니 간단하게 하고 넘어가도록 하겠다.

 

(주의) 기록용으로 작성한 글입니다. 코드가 허접하거나 알고리즘의 효율이 낮을 수 있습니다.

댓글 환영합니다!

반응형

댓글