728x90
https://www.acmicpc.net/problem/25193

문자열이 주어졌을 경우 치킨의 개수와 다른 음식의 개수를 세어 적절히 배치했을때 치킨을 연속으로 먹는것이
최소인 경우를 출력하는 문제이다.

위에 그림처럼 풀었을때 따로 예외처리해야 할 것은 다른 음식이 0일경우를 생각해야한다. 0으로 나눌 수 없으니
만약 다른 음식이 없을 경우는 무조건 치킨만 먹을 수 있으므로 치킨의 개수가 답이된다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e9
int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
string s;
int N, chicken, food;
void solve() {
if(food == 0) {
cout << chicken;
return;
}
if(chicken % (food+1) == 0) {
cout << chicken / (food+1);
}
else {
cout << (chicken / (food+1)) + 1;
}
}
void input() {
cin >> N >> s;
for(int i = 0; i < N; i++) {
if(s[i] == 'C') chicken++;
else food++;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
input();
solve();
}

728x90
'Algorithm' 카테고리의 다른 글
| [백준] 12849번 본대 산책 (C++) (0) | 2025.08.05 |
|---|---|
| [백준] 9024번 두 수의 합 (C++) (0) | 2025.08.04 |
| [백준] 3987번 보이저 1호 (C++) (0) | 2025.08.02 |
| [백준] 20007번 떡 돌리기 (C++) (0) | 2025.08.01 |
| [백준] 4803번 트리 (C++) (0) | 2025.07.31 |