Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1152: 단어의 개수 #15

Open
minsoo0715 opened this issue Sep 4, 2023 · 0 comments
Open

1152: 단어의 개수 #15

minsoo0715 opened this issue Sep 4, 2023 · 0 comments
Assignees
Labels
#문자열 문자열 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들

Comments

@minsoo0715
Copy link
Owner

minsoo0715 commented Sep 4, 2023

1152: 단어의 개수

소스 코드

아이디어

단어의 개수는 공백의 개수로 처리 가능

  • 앞, 뒤 공백이 있는 경우 : $(단어의\;개수) = (공백의\;개수) - 1$
  • 앞, 뒤 중 하나만 공백이 있는 경우: $(단어의\;개수) = (공백의\;개수)$
  • 앞, 뒤 공백이 없는 경우: 단어의 개수 = $(단어의\;개수) = (공백의\;개수) + 1$

구현

공백의 개수를 구하고, 앞 뒤 공백 여부에 따라서 답 계산

int cnt = 0, len = s.length();

for(int i = 0; i<len; ++i) {
    if(s[i] == ' ') ++cnt;
}

if(s[0] == ' ' && s[len-1] == ' ') {
    cnt += -1;
}else if(s[0] != ' ' && s[len-1] != ' ') {
    cnt += 1;
}
cout << cnt;
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 #문자열 문자열 문제 labels Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#문자열 문자열 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들
Projects
None yet
Development

No branches or pull requests

1 participant