[백준] 1152번 - 단어의 개수 [Java][C++]
[백준] 1152번 - 단어의 개수 [Java][C++]
1. 문제 풀이
주어진 입력에서 단어의 개수를 구하는 간단한 문제다.
2. 코드
1. 구현 [Java]
StringTokenizer 를 활용해 공백을 기준으로 파싱한 후 countTokens 메서드로 단어의 개수를 세는 방식으로 해결했다.
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(false);
cin.tie(nullptr);
string s;
int cnt = 0;
while (cin >> s) {
cnt++;
}
cout << cnt;
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 228 ms | 19976 KB | 292 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 24 ms | 3680 KB | 212 B |
This post is licensed under CC BY 4.0 by the author.