[BaekJoon] #10821 - 정수의 개수 [Java][C++][Python]
[BaekJoon] #10821 - 정수의 개수 [Java][C++][Python]
1. 아이디어
숫자와 콤마로 이루어진 문자열 $S$에 포함된 정수의 개수를 구하는 문제로, $S$의 첫 문자와 마지막 문자가 숫자이고 콤마가 연속하지 않아 콤마 사이에 빈 항이 없으므로 정수의 개수는 콤마의 개수보다 1 많고, 이는 콤마로 쪼갠 항의 개수와도 같다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(br.readLine().split(",").length);
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string line;
cin >> line;
cout << count(line.begin(), line.end(), ',') + 1;
}
1
2
3
4
5
import sys
input = sys.stdin.readline
print(input().rstrip().count(",") + 1)
This post is licensed under CC BY 4.0 by the author.