[BOJ/C, C++] 步骤解析 字符串 ~ 二维数组

发布: (2026年1月2日 GMT+8 16:00)
5 min read
原文: Dev.to

I’m happy to translate the article for you, but I need the full text that you’d like translated. Could you please paste the content (excluding the source line you already provided) here? Once I have the text, I’ll translate it into Simplified Chinese while preserving the original formatting and markdown.

2026‑01‑02 整理

从字符串到二维数组,尝试了解决了多个问题。下面整理了每个问题的解法和关键要点。

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 <i

```cpp
ostream>
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) {
            // (코드 생략)
        }
    }
}

这是一种在填充区域的同时进行计数的方式。相比于在输入后再次遍历数组的旧代码,更加高效。
我认为这在学习 fillcount 函数方面具有意义。

Back to Blog

相关文章

阅读更多 »

每周挑战:新年,新挑战

新年快乐,大家。每周,Mohammad S. Anwar 会发布 The Weekly Challenge https://theweeklychallenge.org/,这是一个让我们所有人提出解决方案的机会……