[BaekJoon] 27964번 - 콰트로치즈피자 [Java][C++]
[BaekJoon] 27964번 - 콰트로치즈피자 [Java][C++]
1. 문제 풀이
콰트로치즈피자는 서로 다른 네 종류의 치즈 토핑이 들어가야 하며 치즈 토핑은 이름이 Cheese로 끝난다. 집합 자료구조를 활용해서 이름이 Cheese로 끝나는 토핑들을 담은 후 집합의 크기가 4 이상인지 구하면 해결할 수 있다.
2. 코드
1. 풀이 [Java]
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
Set<String> set = new HashSet<>();
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
while (n-- > 0) {
String s = st.nextToken();
if (s.endsWith("Cheese")) {
set.add(s);
}
}
if (set.size() >= 4) {
System.out.println("yummy");
} else {
System.out.println("sad");
}
}
}
2. 풀이 [C++]
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
unordered_set<string> st;
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (string(s.end() - 6, s.end()) == "Cheese") {
st.insert(s);
}
}
if (st.size() >= 4) {
cout << "yummy";
} else {
cout << "sad";
}
}
This post is licensed under CC BY 4.0 by the author.