백준 1159 농구 경기

CJB_ny·2022년 12월 29일
0

백준

목록 보기
25/104
post-thumbnail

구현문제임

문제

농구경기


int <-> char 간의 암시적 형변환과 아스키 코드를 알고있는지 물어보는 문제인거같다.

내풀이

#include <iostream>
#include <vector>
using namespace std; 

#define MAX 151

string arr[MAX];
int Count[26];

int main()
{
	int n;
	cin >> n;

	for (int i = 1; i <= n; ++i) cin >> arr[i];

	for (int i = 1; i <= n; ++i) Count[arr[i][0] - 'a']++;

	int c = 0;
	for (int i = 0; i < 26; ++i)
	{
		if (Count[i] >= 5) cout << char(97 + i);
		else ++c;
	}

	if (c == 26) cout << "PREDAJA";

	return 0;
}

나는 이렇게 그냥 바로 char(97 + i);를 형변환해서 출력했는데 아래 풀이의 코드가 더 깔끔하고

c++의 암시적 형변환을 잘 활용한 코드같다.

좀더 배우도록 하자..

풀이

#include <iostream>
#include <vector>
using namespace std; 

#define MAX 151

string arr[MAX];
int Count[26];

int main()
{
	int n;
	cin >> n;

	for (int i = 1; i <= n; ++i)
	{
		string s;
		cin >> s;
		Count[s[0] - 'a']++;
	};

	string ret;
	for (int i = 0; i < 26; ++i) if (Count[i] >= 5) ret += (i + 'a');

	if (ret.size()) cout << ret;
	else cout << "PREDAJA";

	cout << 'a' + 1;


	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글