알고리즘/백준
[백준] 1152번 단어의 개수
JeongHyeon
2017. 12. 9. 00:02
문제
출처 : https://www.acmicpc.net/problem/1152
풀이과정
어제 새벽에 잠들어서 낮잠을 오래오래 자느라 늦게 풀었다.
단어의 개수라고 해서 띄어쓰기로 구분해서 단어를 세려고 했다.
하지만 맨 앞이 공백이거나 마지막이 공백인 경우의 예외처리를 하지않아 좀 오래 걸렸다.
소스코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> #include <cstring> using namespace std; #define SIZE 1000000 int main() { char std[SIZE]; int cnt = 0; cin.getline(std,SIZE); for (int i = 0; i < strlen(std); i++) { if (std[i-1]!=32 && std[i] == 32 ) cnt++; } if (std[0] == ' ') cnt--; if (std[strlen(std) - 1] == ' ') cnt--; cout << ++cnt; return 0; } | cs |