알고리즘 역량테스트 문제/SWEA
[SWEA][D2][c++] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기
coooding
2020. 7. 20. 20:44
문제
1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기
문제 링크
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13zo1KAAACFAYh
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이 과정
1. 1000개 값을 입력받습니다.
2. 배열을 사용하여 입력받은 수를 인덱스로 배열의 값을 1씩 올립니다.
3. 가장 값이 높은 배열의 인덱스를 출력합니다.
코드
#include<iostream>
#include<cstring>
using namespace std;
int main(int argc, char** argv)
{
cin.tie(0);
cout.tie(0);
cout.sync_with_stdio(false);
int test_case;
int T;
cin>>T;
int arr[101];
for(test_case = 1; test_case <= T; ++test_case)
{
cin>>test_case;
memset(arr,0,sizeof(arr));
int temp;
int max=0;
int maxIndex=0;
for(int i=0; i<1000; i++){
cin >> temp;
arr[temp]++;
if(arr[temp]>max){
max=arr[temp];
}
}
for(int i=100; i>=0; i--){
if(max==arr[i]){
maxIndex=i;
break;
}
}
cout<<"#"<<test_case<<" "<<maxIndex<<"\n";
}
return 0;
}