17608번 - 막대기(c++)

Duna·2020년 10월 23일
0

🗒 17608번 문제

📌 막대기를 오른쪽에서 바라봤을 때 보이는 막대기를 찾아라

1️⃣ 왼쪽부터 보이는 막대기를 찾기 보단 오른쪽에서 보이는 막대기를 찾자!

2️⃣ 맨 왼쪽 막대기를 'top'으로 두고 해당 막대기보다 더 큰 막대기가 있다면 'top'를 갱신해주기

3️⃣ 'top'보다 크다면 해당 sticks값을 count하기 -> 'top'과 같은 건 세지 않기, 보이지 않기에❗️

4️⃣ count한 Stick를 출력하기


➰ 코드로 나타낸 17608번 ➰

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int stickNumber = 0, countStick = 1;
    // 전체 stick 수, 오른쪽에서 보이는 stick 수
    
    cin >> stickNumber;
    
    int sticks[stickNumber];
    
    for (int i = 0; i < stickNumber; i++)
        cin >> sticks[i];
        // 전체 stick의 길이를 받아오기
    
    int top = sticks[stickNumber-1];
    // 맨 오른쪽 막대기를 top으로 설정
    
    for (int i = stickNumber-2; i >= 0; i--) {
    	// top보다 크다면 count 세고 해당 막대기를 top으로 설정
        if(sticks[i] > top){
            countStick++;
            top = sticks[i];
        }
    }
    
    cout << countStick << endl;
}
profile
더 멋진 iOS 개발자를 향해

0개의 댓글