티스토리 뷰
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제풀이
10개의 테스트 케이스마다 테스트 케이스 번호, 1000명의 점수가 주어진다.
최빈수를 찾기 위해서 cnt 배열에 각 점수가 몇 번 나타나는지 카운트해준다.
최빈수가 여러 개 일 때에는 가장 큰 점수를 출력하라고 했으므로 cnt[i]가 maxCnt 이상일 경우에 갱신해준다.
코드
C++ 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int cnt[101]; | |
int main() | |
{ | |
int T, k; | |
cin>>T; | |
for (int test_case = 1; test_case <= T; ++test_case) { | |
cin >> test_case; | |
cout << "#" << test_case << " "; | |
fill_n(cnt, 101, 0); | |
for (int i = 0; i < 1000; i++) { | |
cin >> k; | |
cnt[k]++; | |
} | |
int answer = 0; | |
int maxCnt = 0; | |
for (int i = 0; i <= 100; i++) { | |
if (cnt[i] >= maxCnt) { | |
answer = i; | |
maxCnt = cnt[i]; | |
} | |
} | |
cout << answer << "\n"; | |
} | |
return 0; | |
} |
Java 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
class Solution { | |
public static void main(String[] args) throws Exception { | |
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | |
int T = Integer.parseInt(bf.readLine()); | |
for (int test_case = 1; test_case <= T; test_case++) { | |
test_case = Integer.parseInt(bf.readLine()); | |
System.out.print("#" + test_case + " "); | |
int[] cnt = new int[101]; | |
StringTokenizer st = new StringTokenizer(bf.readLine(), " "); | |
while (st.hasMoreTokens()) { | |
cnt[Integer.parseInt(st.nextToken())]++; | |
} | |
int answer = 0; | |
int maxCnt = 0; | |
for (int i = 0; i <= 100; i++) { | |
if (cnt[i] >= maxCnt) { | |
answer = i; | |
maxCnt = cnt[i]; | |
} | |
} | |
System.out.println(answer); | |
} | |
} | |
} |
'Problem Solving > SWEA' 카테고리의 다른 글
[SWEA 1220] Magnetic C++/Java (0) | 2021.07.28 |
---|---|
[SWEA 1210] Ladder1 C++/Java (0) | 2021.07.28 |
[SWEA 1209] Sum C++/Java (0) | 2021.07.28 |
[SWEA 1208] Flatten C++/Java (0) | 2021.07.28 |
[SWEA 1206] View C++/Java (0) | 2021.07.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준
- 구현
- 배열
- Two Pointer
- 이분 탐색
- 문자열
- 알고리즘
- 정렬
- C++
- 자바
- BFS
- dfs
- 큐
- 스택
- SW Expert Academy
- 두 포인터
- 투 포인터
- 트리
- Kotlin
- 재귀
- 브루트포스
- 프로그래머스
- 그래프
- 위상 정렬
- algorithm
- Java
- programmers
- SWEA
- BOJ
- 분할 정복
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함