민희의 코딩일지

[백준] 1159 - 농구 경기 본문

자료구조, 알고리즘

[백준] 1159 - 농구 경기

heehminh 2023. 9. 27. 17:42
반응형

https://www.acmicpc.net/problem/1159

 

1159번: 농구 경기

상근이는 농구의 세계에서 점차 영향력을 넓혀가고 있다. 처음에 그는 농구 경기를 좋아하는 사람이었다. 농구에 대한 열정은 그를 막을 수 없었고, 결국 상근이는 농구장을 청소하는 일을 시작

www.acmicpc.net

알고리즘

구현, 문자열 

 

풀이 (1)

key의 맨 앞의 한 글자만 잘라 map 에 넣어준다.

map의 key 중에 그 글자가 있다면 value+1 을 해주고, 없다면 초기값 1로 세팅해준다.

for(int i=0; i<N; i++) {
    key = players[i].substr(0, 1);
    if (mp.find(key) != mp.end()) {
        mp[key]++;
    } else {
        mp[key] = 1;
    }
}

value 가 4 초과라면 성을 출력, 아니라면 PREDAJA를 출력

for(auto it=mp.begin(); it != mp.end(); it++) {
    if (it->second > 4) {
        cout << it->first;
        isSelect=true;
    } 
}

if(isSelect==false) {
    cout << "PREDAJA";
}

전체코드 (1) 

#include <bits/stdc++.h>
using namespace std;
int N;
vector<string> players;
string player, key;
map<string, int> mp;
bool isSelect;

int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	
	isSelect = false; 
	cin >> N;
	for (int i=0; i<N; i++) {
		cin >> player;
		players.push_back(player);
	}
	
	for(int i=0; i<N; i++) {
		key = players[i].substr(0, 1);
		if (mp.find(key) != mp.end()) {
			mp[key]++;
		} else {
			mp[key] = 1;
		}
	}
	
	
	for(auto it=mp.begin(); it != mp.end(); it++) {
		if (it->second > 4) {
			cout << it->first;
			isSelect=true;
		} 
	}
	
	if(isSelect==false) {
		cout << "PREDAJA";
	}
	// PREDAJA
	
	return 0;
}

 

 

반응형

'자료구조, 알고리즘' 카테고리의 다른 글

[백준] 2559 - 수열  (0) 2023.09.27
Comments