[백준] 1157번. 단어 공부

leeeha·2021년 9월 30일
0

백준

목록 보기
11/185

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

tolower, toupper 함수

https://artist-developer.tistory.com/29

// 대소문자 변환 함수 직접 구현하기
#include <iostream>
#include <string>
using namespace std;

int lower(int c) {
	if (c >= 'A' && c <= 'Z') {
		c = c - 'A' + 'a';
	}
	return c;
}

int upper(int c) {
	if (c >= 'a' && c <= 'z') {
		c = c - 'a' + 'A';
	}
	return c;
}

int main() {
	string str = "Hello World";

	int i = 0;
	char c;
	while (str[i]) {
		c = lower(str[i]);
		putchar(c);
		i++;
	}

	return 0;
}

https://www.cplusplus.com/reference/cctype/tolower/
https://blockdmask.tistory.com/452

/* tolower example */
#include <stdio.h>
#include <ctype.h>
int main()
{
	int i = 0;
	char str[] = "Test String.\n";
	char c;

	while (str[i])
	{
		c = str[i];
		putchar(tolower(c));
		i++;
	}

	return 0;
}

문제 풀이

문자열에서 "각 문자가 등장한 횟수"를 어떻게 세야 할지 알아내지 못해서 결국 다른 블로그를 참고했다.
https://cryptosalamander.tistory.com/12

대문자 알파벳에서 'A'를 빼주면, 그 자체가 알파벳 순서에 따른 인덱스가 된다는 아이디어를 떠올리지 못했다! 아스키코드가 이렇게 쓰이는구나!

// 1157번. 단어 공부
#include <iostream>
#include <string>
#include <cctype> // tolower, toupper
using namespace std;

int main() {
	string str;
	cin >> str;

	int cnt[26] = { 0 }; // 0으로 초기화

	for (int i = 0; i < str.length(); i++){
		// 대소문자 구분 없애기 위해 모두 대문자로 변환
		str[i] = toupper(str[i]);

		// 문자열에서 "각 문자가 등장한 횟수"를 어떻게 셀까?
		// 대문자 알파벳에서 'A'를 빼주면, 그 자체가 알파벳 순서에 따른 인덱스가 된다!
		// A는 0, Z는 25 (인덱스에 'A'를 더하면 다시 알파벳이 나온다)
		cnt[str[i] - 'A']++;
	}

	// 가장 많이 등장한 알파벳의 인덱스 기억하기
	int maxCount = cnt[0];
	int idx = 0;
	for (int i = 1; i < 26; i++) {
		if (maxCount < cnt[i]) {
			maxCount = cnt[i];
			idx = i;
		}
	}

	int tmp = 0;
	for (int i = 0; i < 26; i++) {
		// 최댓값이 중복되지 않는지 검사하기
		if (maxCount == cnt[i])
			tmp++;
	}

	// 최댓값이 2번 이상 나오면 '?' 출력
	if (tmp > 1) cout << "?";
	// 그렇지 않으면 가장 많이 등장한 알파벳 출력하기
	else cout << (char)(idx + 'A');
	
	return 0;
}
profile
Keep Going!

0개의 댓글