본문 바로가기

프로그래밍/Baekjoon

(C언어) 백준 알고리즘 1157번 단어 공부

문제

풀이 (C언어)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
#define MAX 1000000
 
void upper(char *str);
 
int alpha[26= {0,};
 
int main(){
    char word[MAX];
    int i, index;
    int max = 0;
    int flag = 0;
    int len;
    scanf("%s", word);
    upper(word);
    
    len = strlen(word);
    for(i = 0; i < len; i++){
        index = word[i] - 65;
        alpha[index] +=1;
    }
 
    for(i = 0; i< 26; i++){
        if(alpha[i] > max){
            max = alpha[i];
            index = i;
        }
    }
 
    for(i = 0; i<26; i++){
        if(i == index) continue;
        if(max == alpha[i]){
            flag = 1;
        }
    }
    
    if(flag){
        printf("?");
    }else{
        printf("%c", index + 65);
    }
 
    return 0;
}
 
void upper(char *str){
    int i;
    int len = strlen(str);
    for (i = 0; i < len; i++){
        if (islower(str[i])){
            str[i]= toupper(str[i]);
        }
    }
}
 
cs

문제 출처