1107번 - 리모컨(c++)

Duna·2020년 10월 24일
0

🗒 1107번 문제

📌 최댓값까지 올라가면서 target을 찾아내기

1️⃣ 기존 채널인 100에서 targetChannel로 가는 게 빠른지

2️⃣ 아니면 입력 채널에서 targetChannel로 가는 게 빠른지

3️⃣ 문제의 범위는 5000000이지만 위에서 내려오는 경우를 생각해서 1000000까지만 loop 돌리기

4️⃣ 함수에 들어온 버튼의 값이 true라면 false return, 아니면 true return

5️⃣ min함수를 통해서 1️⃣, 2️⃣번 중에 더 작은 값을 출력하도록 한다


➰ 코드로 나타낸 1107번 ➰

#include <iostream>

using namespace std;

bool checkChannel(int currentChannel, bool buttons[10]) {
    string str = to_string(currentChannel);
    
    for (int i = 0; i < str.length(); i++) {
        if (buttons[str[i] - 48])
            return false;
    }
    
    return true;
}

int main() {
    int targetChannel;
    int buttonSize;
    bool buttons[10] = { false };
    
    cin >> targetChannel >> buttonSize;
    string targetString = to_string(targetChannel);
    int minNum = abs(targetChannel - 100);
    // 기존 채널 100에서 targetChannel로 +/-
    
    for (int i = 0; i < buttonSize; i++) {
        int num;
        cin >> num;
        buttons[num] = true;
    }
    
    for (int i = 0; i <= 1000000; i++)  {
        if(checkChannel(i, buttons)){
            int num = abs(targetChannel-i) + to_string(i).length();
            // 입력 채널에서 targetChannel로 +/-
            minNum = min(minNum, num);
        }
    }
    
    cout << minNum << endl;
}
profile
더 멋진 iOS 개발자를 향해

0개의 댓글