[BOJ/C, C++] 단계별로 풀어보기 문자열 ~ 2차원 배열

Published: (January 2, 2026 at 03:00 AM EST)
5 min read
Source: Dev.to

Source: Dev.to

2026‑01‑02 일자 정리

문자열부터 2차원 배열까지 여러 문제를 풀어보았습니다. 아래에 각 문제별 풀이와 핵심 포인트를 정리했습니다.

1. 1081번 숫자 합

문제 링크: (링크 삽입)

// C++ (iostream)
#include <iostream>
#include <string>
using namespace std;

int main() {
    int N, res = 0;
    string num;
    cin >> N >> num;
    for (int c : num)               // 문자열을 순회
        res += c - '0';             // '0'~'9' → 0~9
    cout << res;
}
int main() {
    int N, res = 0;
    scanf("%d", &N);
    while (N--) {
        int t;
        scanf("%1d", &t);   // 한 자리씩 읽음
        res += t;
    }
    printf("%d", res);
}
  • %1d 를 사용하면 한 문자(한 자리)만 입력받는다.
  • 조건이 바뀔 가능성이 있으면 string 을 쓰는 것이 안전하다.

2. 1152번 단어의 개수

문제 링크: (링크 삽입)

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

int main() {
    string str;
    getline(cin, str);          // 한 줄 전체 입력
    int cnt = 1;                 // 최소 한 단어가 있다고 가정
    for (size_t i = 0; i < str.size(); ++i) {
        // (코드 생략)
    }
    cout << cnt;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string pad[8] = { "ABC", "DEF", "GHI", "JKL",
                     "MNO", "PQRS", "TUV", "WXYZ" };
    string T;  cin >> T;
    int n = 0;

    for (char c : T) {
        for (int i = 0; i < 8; ++i) {
            // (코드 생략)
        }
    }
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string line;
    while (getline(cin, line))          // EOF까지 읽음
        cout << line << '\n';
}
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int cnt[26] = {0};
    string T;  cin >> T;

    for (int c : T)
        ++cnt[ (c >= 'a') ? c - 'a' : c - 'A' ];

    int maxCnt = *max_element(cnt, cnt + 26);
    if (count(cnt, cnt + 26, maxCnt) >= 2)
        cout << "some output";
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string croatia[] = { "c=", "c-", "dz=", "d-", "lj",
                        "nj", "s=", "z=" };
    string T;  cin >> T;

    for (string s : croatia) {
        while (T.find(s) != string::npos)
            T.replace(T.find(s), s.length(), "*");   // 하나씩 * 로 대체
    }
    cout << T;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    int N;  cin >> N;
    int answer = N;

    while (N--) {
        string T;  cin >> T;
        bool seen[26] = {0};
        char prev = 0;

        for (char c : T) {
            if (c != prev) {                 // 새 문자 등장
                if (seen[c - 'a']) {        // 이미 등장했으면 그룹 단어 아님
                    --answer;
                    break;
                }
                seen[c - 'a'] = true;
                prev = c;
            }
        }
    }
    cout << answer;
}
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<vector<int>> a(9, vector<int>(9));
    for (int i = 0; i < 9; ++i)
        for (int j = 0; j < 9; ++j)
            cin >> a[i][j];

    int maxVal = -1, row = 0, col = 0;
    for (int i = 0; i < 9; ++i) {
        for (auto it = a[i].begin(); it != a[i].end(); ++it) {
            if (*it > maxVal) {
                maxVal = *it;
                row = i + 1;
                col = it - a[i].begin() + 1;
            }
        }
    }
    cout << maxVal << " " << row << " " << col;
}
using namespace std;

int main() {
    int maxVal = -1, row = 0, col = 0;
    for (int i = 1; i <= 9; ++i) {
        for (int j = 1; j <= 9; ++j) {
            int x; 
            cin >> x;
            if (x > maxVal) {
                maxVal = x;
                row = i;
                col = j;
            }
        }
    }
    cout << maxVal << " " << row << " " << col;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s[5];
    for (int i = 0; i < 5; ++i)
        cin >> s[i];

    for (int col = 0; col < 5; ++col) {
        // (코드 생략)
    }
}
#include <iostream>
using namespace std;

int main() {
    bool board[100][100] = {0};
    int N;  cin >> N;

    while (N--) {
        int x, y;  cin >> x >> y;
        for (int i = x; i < 100; ++i) {
            // (코드 생략)
        }
    }
}

구역을 채움과 동시에 카운트를 진행하는 방식입니다. 입력 이후 배열을 다시 훑는 전 코드보다 효율합니다.
fill, count 함수를 학습했다는 점에서 의미가 있다고 봅니다.

Back to Blog

Related posts

Read more »