백준 1152

HR·2022년 4월 22일
0

알고리즘 문제풀이

목록 보기
18/50

백준 1152 : 단어의 개수

  1. 줄 전체 입력받기
  2. 공백이면 건너뜀 (세는중) 변수를 false로 업데이트
  3. (세는중) 변수가 false이고 공백이 아니면 count를 증가시키고, (세는중) 변수를 true로

정답 코드

#include <iostream>
#include <vector>

using namespace std;

string s;
int isCounting=0, cnt;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	getline(cin, s);
	
	for(int i=0; i<s.size(); i++) {
		if(s[i]==' ') {
			isCounting = 0;
		}
		else if(s[i]!=' ' && isCounting==0) {
			cnt++;
			isCounting = 1;
		}
	}
	
	cout<<cnt<<'\n';
	
	return 0;
}
  • c++에서 단어들 split 하는 방법도 알아놔야 겠다

0개의 댓글