corona3235   2년 전


#include <stdio.h>

#include <stdli.h>

#include <string.h>

int main() {

char s[1000001];

scanf("%s", s);

int len = strlen(s);

// 동적 할당을 사용하여 count 배열 초기화

int* count = (int*)malloc(len * sizeof(int));

if (count == NULL) {

perror("Failed to allocate memory");

return 1;

}

// 대문자로 변환 및 count 배열 초기화

for (int i = 0; i < len; i++) {

if (s[i] >= 'a' && s[i] <= 'z')

s[i] = s[i] - ('a' - 'A');

count[i] = 0;

}

// 문자 빈도수 계산

for (int i = 0; i < len; i++) {

for (int k = 0; k < len; k++) {

if (s[i] == s[k])

count[i]++;

}

}

// 가장 많이 나온 횟수 구하기

int max = 0;

for (int i = 0; i < len; i++) {

if (max < count[i])

max = count[i];

}

// 가장 많이 나온 글자의 인덱스 찾기

int index = -1;

for (int i = 0; i < len; i++) {

if (max == count[i]) {

if (index == -1) {

index = i;

} else if (s[index] != s[i]) {

printf("?");

free(count);

return 0;

}

}

}

// 결과 출력

printf("%c", s[index]);

free(count);

return 0;

}

댓글을 작성하려면 로그인해야 합니다.