반응형

문제출처

https://programmers.co.kr/learn/courses/30/lessons/42840


문제풀이

배열을 이용해 수포자가 찍는 방식을 입력하고 mod연산을 통해 정답갯수를 알아내는 문제


소스코드


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
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> supoja1={1,2,3,4,5}; // 5
    vector<int> supoja2={2,1,2,3,2,4,2,5}; // 8
    vector<int> supoja3={3,3,1,1,2,2,4,4,5,5}; // 10
 
    int cnt1=0, cnt2=0, cnt3=0;
    int idx1=0, idx2=0, idx3=0;
 
    for(int i=0; i<answers.size(); i++) {
        idx1=i%5, idx2=i%8, idx3=i%10;
        (answers[i] == supoja1[idx1]) ? ++cnt1 : cnt1;
        (answers[i] == supoja2[idx2]) ? ++cnt2 : cnt2;
        (answers[i] == supoja3[idx3]) ? ++cnt3 : cnt3;
    }
 
    int maxCnt = max(cnt1, max(cnt2, cnt3));
    if(maxCnt == cnt1) answer.push_back(1);
    if(maxCnt == cnt2) answer.push_back(2);
    if(maxCnt == cnt3) answer.push_back(3);
 
    return answer;
}
cs


반응형

'Algorithm' 카테고리의 다른 글

[BOJ 10872] 팩토리얼  (0) 2019.07.03
[프로그래머스 12936] 줄 서는 방법  (0) 2019.04.23
[BOJ 1655] 가운데를 말해요  (0) 2019.04.16
[BOJ 1157] 단어공부  (0) 2019.04.15
[BOJ 2675] 문자열 반복  (0) 2019.04.15

+ Recent posts