개발 이야기 안하는 개발자

코딩 테스트 공부_2 본문

개발/코딩 테스트

코딩 테스트 공부_2

07e 2026. 1. 13. 23:14
반응형

백준 11723

문제를 보면 x의 값은 항상 1부터 20사의 값만 추정된다.

그래서 처음부터 20개의 값을 셋팅하고 있으면 true로 바꾸는 trigger 형식의 배열을 미리 설정해두었다.

 

코드에는 문제가 없는것같은데 계속 시간초과가 나길래 찾아보았다.

    ios::sync_with_stdio(false);
    cin.tie(NULL);

이 두줄을 넣으니 성공했다.

ios::sync_with_stdio(false); C++ 스트림(cin,cout)과 C 스트림(stdin,stdout)의 동기화를 끔 C와 C++이 같은 버퍼를 공유 → 동기화 비용 발생 끄면 C++만 독립 버퍼 사용 → 오버헤드 대폭 감소 scanf/printf와 cin/cout을 섞어 쓰면 안 됨 (예측 불가능한 결과 나올 수 있음)
cin.tie(NULL); (또는 cin.tie(nullptr);) cin이 cout과 **연결(tie)**되어 있는 상태를 끊음 cin으로 입력 받기 전에 cout 버퍼를 자동으로 flush하는 기능이 기본 이걸 끄면 불필요한 flush가 사라짐 인터랙티브 문제(실시간 출력 확인 필요)에서는 출력이 늦게 보일 수 있음 (거의 안 씀)

 

앞으로 문제 풀때는 기본으로 추가해야겠다/

 

 

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <fstream>
#include <stack>
#include <math.h>
#include <cstring>
#include <climits>
#include <sstream>

using namespace std;

int main() {

    //Input Reader///////////////////
    streambuf* originalCin = cin.rdbuf();
    ifstream inputFile("input.txt");
    if (!inputFile.is_open()) {
        cerr << "파일을 열 수 없습니다." << endl;
        return 1;
    }
    cin.rdbuf(inputFile.rdbuf());

    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    //////////////////////////////////////

    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int M;
    cin >> M;

    bool S[21] = {};

    string op;
    int x;

    for (int i = 0; i < M; i++) 
    {
        cin >> op;

        if (op == "add") 
        {
            cin >> x;
            S[x] = true;
        }
        else if (op == "remove") 
        {
            cin >> x;
            S[x] = false;
        }
        else if (op == "check") 
        {
            cin >> x;
            cout << S[x] << '\n';
        }
        else if (op == "toggle") 
        {
            cin >> x;
            S[x] = !S[x];
        }
        else if (op == "all") 
        {
            memset(S, true, sizeof(S));
        }
        else if (op == "empty") 
        {
            memset(S, false, sizeof(S));
        }
    }

    return 0;
}
반응형

'개발 > 코딩 테스트' 카테고리의 다른 글

코딩 테스트 공부_1  (0) 2026.01.12