Post

[BaekJoon] 1152번 - 단어의 개수 [Java][C++]

[BaekJoon] 1152번 - 단어의 개수 [Java][C++]

문제 링크


1. 아이디어


주어진 입력에서 단어의 개수를 구하는 간단한 문제다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println(new StringTokenizer(br.readLine()).countTokens());
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string s;
    int cnt = 0;

    while (cin >> s) {
        cnt++;
    }

    cout << cnt;
}

3. 디버깅


공백 하나만 주어지는 입력이 있는 것으로 보인다. 해당 경우 0을 출력하도록 해주니 통과가 됐다.


4. 참고


없음.


This post is licensed under CC BY 4.0 by the author.