BOJ::1157 단어 공부
https://www.acmicpc.net/problem/1157
String만 잘 다룰 줄 알면 풀 수 있는데 String처리가 익숙하지가 않다..
JAVA
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main{ public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122 int a[] = new int[27]; String s = br.readLine(); int n = s.length(); int max = 0; for(int i = 0 ; i < n ; i++){ int c = (int)s.charAt(i); //소문자이면 대문자로 변환 if(c>=97 && c<=122){ c -=32; } c -='A'; a[c]++; //가장 많이 나온 알파벳의 빈도수를 max 에 저장 if(a[c]>max){ max = a[c]; } } int cnt = 0; int res = 0; for(int i = 0 ; i < 27 ; i++){ if(a[i]==max){ cnt++; res = 'A'+i; //가장 많이나온 알파벳이 2개 이상이면 ? 출력 if(cnt==2){ System.out.println("?"); return; } } } System.out.println((char)res); } } |
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 | #include <iostream> #include <string> using namespace std; int a[27]; string s; int main() { //'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122 cin >> s; int n = s.length(); int max = 0; for (int i = 0; i < n; i++) { int c = s.at(i); if (c >= 97 && c <= 122) { c -= 32; } c = c - 'A'; a[c]++; if (a[c] > max) { max = a[c]; } } int cnt = 0; int res = 0; for (int i = 0; i < 27; i++) { if (a[i] == max) { res = i + 'A'; cnt++; if (cnt == 2) { cout << "?" << endl; return 0; } } } cout << (char)res << endl; } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
1260 DFS와 BFS (0) | 2017.12.31 |
---|---|
1157 이상한 곱셈 (0) | 2017.12.30 |
1152 단어의 개수 (0) | 2017.12.30 |
1149 RGB거리 (0) | 2017.12.30 |
1065 한수 (0) | 2017.12.30 |