백준 문제를 풀면서 처음으로 객체를 써보았다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Main {
//알파벳 객체
public static class Alphabet {
char s;
int count;
public char getS() {
return s;
}
public void setS(char s) {
this.s = s;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void countUp () {
count++;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String st = br.readLine();
st = st.toUpperCase();
ArrayList<Alphabet> abc = new ArrayList<>();
for(int i=0; i<st.length(); i++ ) {
boolean flag = false;
//abc 리스트에 이미 생성된 객체가 있다면 카운트 업
for( Alphabet a : abc ) {
if(a.getS() == st.charAt(i)){
a.countUp();
flag = true;
break;
}
}
//만약 어떤 alphbet 객체도 만나지 못했다면 객체 생성
if(flag == false) {
Alphabet alpha = new Alphabet();
alpha.setS(st.charAt(i));
alpha.countUp();
abc.add(alpha);
}
}
int num = 0;
char most = ' ';
for( Alphabet a : abc) {
if(a.getCount() > num) {
num = a.getCount();
most = a.getS();
} else if(a.getCount() == num) {
most = '?';
}
}
bw.write(most);
bw.flush();
bw.close();
}
}
'Data Structure & Algorithm > 백준' 카테고리의 다른 글
No2798 블랙잭(브루트포스 알고리즘) (0) | 2022.10.25 |
---|---|
백준 2164번 카드2 (Queue) (0) | 2022.10.25 |
이진 검색 (10816번, 1920번) (0) | 2022.09.29 |
solved.ac 활용하기 (0) | 2022.09.27 |
1152번 : 단어의 개수 (String의 split 메소드 주의사항) (0) | 2022.09.19 |