1)
#include <bits/stdc++.h>
using namespace std;
int num;
string name;
vector<string> names;
vector<char> memberlist;
map<char, int> mp;
bool isValue = false;
int main(){
cin >> num;
for(int i=0; i<num; i++){
cin >> name;
names.push_back(name);
}
for(int i=0; i<num; i++){
mp[names[i][0]]++;
}
for(auto it : mp){
if(it.second >= 5){
memberlist.push_back(it.first);
isValue = true;
}
}
if(isValue){
for(char i : memberlist) cout << i;
}else{
cout << "PREDAJA";
}
/*if(memberlist.empty()){
cout << "PREDAJA";
}else{
for(char i : memberlist) cout << i;
}*/
return 0;
}
같은 문자를 찾고 카운트하는 문제다.
저번에 풀었던 백준 1157번- 단어공부의 풀이와 유사하다.
이중 for문이 아니라 map을 이용하여 key-value로 문제를 풀면 더 간단하게 풀리는 것 같다.
2) 배열 이용
#include <bits/stdc++.h>
using namespace std;
int n;
int eng[30]; //빈배열
string name;
bool check = true;
int main(){
cin >> n;
//eng 배열 채우기
for(int i=0; i<n; i++){
cin >> name;
int j = name[0]-'a';
eng[j]++;
}
for(int i=0; i<26; i++){
if(eng[i] >=5){
cout << (char)(i +'a');
check=false;
}
}
if(check){
cout << "PREDAJA";
}
return 0;
}
a-z의 갯수를 셀 eng라는 배열을 만들어놓고 이름의 첫글자를 카운드한다.
1번과 유사하지만 1번은 map을 이용했고, 2번은 배열을 이용했다.
'코테' 카테고리의 다른 글
백준 1212번 - 8진수 2진수 (1) | 2023.08.04 |
---|---|
백준 1173번-운동 (0) | 2023.07.31 |
백준 1157번 - 단어 공부 (0) | 2023.07.28 |
백준 1110번-사이클 (0) | 2023.07.28 |
백준 1051번- 숫자 정사각형 (0) | 2023.07.27 |