# 백준 : 단어의 갯수(1152)
문제
https://www.acmicpc.net/problem/1152소스
(Github: https://github.com/wonjnlee/wallydev/blob/master/bj_1152_numberofword)#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main(void) {
string str;
int count = 0;
getline(cin,str);
if(str.compare("") == 0) cout<<"0"<<endl;
else {
for(int i=0; i<str.length(); i++) {
if(str.at(i) == ' ') count++;
}
if(str.at(0) == ' ') count = count-1;
if(str.at(str.length()-1) == ' ') count = count-1;
cout<<count+1<<endl;
}
return 0;
}
풀이
진짜 말그래도 '뻘짓을 쌓고 쌓아 바벨탑만큼 쌓은 다음' 해결한 문제다.사실 어떻게 풀어야할지는 명확하게 알고 있었는데, 왜 코드로만 구현하면 오류가 나는지;;
처음에는 개행('\n') 문자 처리를 해야하는건가 싶어서 그 위주로 진행했는데 해결 실패.
굉장히 단순하게 맨앞과 맨뒤에 스페이스(' ') 처리 만 해주니까 성공..
정답률이 20%정도 되어서 어려울거라고 부담을 가져서 그런가 실제로는 굉장히 간단했음.
댓글
댓글 쓰기