프로그래머스_완전탐색_모의고사[C++]
레벨 1문제이기도 하고 쉽게 풀어보자 했는데 은근히 오래걸렸다. 패턴들을 분석하여 간결하게 코드를 짜보고자 했는데 더욱 더러워졌다. 처음 짠 코드이다.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
struct Op{
bool operator()(pair<int,int>a,pair<int,int>b){
if(a.first == b.first)
return a.second<b.second;
else return a.first>b.first;
}
};
vector<int> solution(vector<int> answers) {
vector<int> answer;
vector<int>ans1(10001);
vector<int>ans2(10001);
vector<int>ans3(10001);
int value = 1;
for(int i=0;i<10001;i++){//1번 수포자
if(value % 5 == 0 )
value = 5;
else value %=5;
ans1[i] = value++;
}
int tmp2 = 1;
for(int i=0;i<10001;i++){ //2번 수포자
if(i%2==0)
ans2[i] = 2;
else{
if(tmp2 == 2)
tmp2 = 3;
else if(tmp2 %5 == 0) tmp2= 5;
else tmp2 %=5;
ans2[i] = tmp2++;
}
}
int arr[10] ={3,3,1,1,2,2,4,4,5,5};
for(int i=0;i<10000;i+=2){
ans3[i] = arr[i%10];
ans3[i+1] = arr[i%10];
}
vector<pair<int,int>> v;
v.push_back({0,1});
v.push_back({0,2});
v.push_back({0,3});
for(int i=0;i<answers.size();i++){
if(answers[i] == ans1[i])
v[0].first++;
if(answers[i]==ans2[i])
v[1].first++;
if(answers[i]==ans3[i])
v[2].first++;
}
sort(v.begin(),v.end(),Op());
if(v[0].first!=v[1].first)
answer.push_back(v[0].second);
else if(v[0].first==v[1].first&&v[1].first!=v[2].first){
answer.push_back(v[0].second);
answer.push_back(v[1].second);
}
else{
answer.push_back(v[0].second);
answer.push_back(v[1].second);
answer.push_back(v[2].second);
}
return answer;
}
코드를 대충봐도 알 수 있겠지만 거지같은 코드이다.. 메모리 낭비 패턴 분석하다가 박살난 가독성… 이거 진짜에요? 어찌어찌 accept은 됐지만 다시 짜보자
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
44
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
struct Op{ // 맞춘 횟수는 내림 차순으로 정렬, 맞춘 횟수가 같다면 학생 번호를 오름차순으로 정렬
bool operator()(pair<int,int> a,pair<int,int>b){
if(a.first==b.first)
return a.second<b.second;
else return a.first>b.first;
}
};
vector<int> solution(vector<int> answers) {
vector<int> answer;
int ans1[] = {1,2,3,4,5};
int ans2[] = {2,1,2,3,2,4,2,5};
int ans3[] = {3,3,1,1,2,2,4,4,5,5};
vector<pair<int,int>>correct(3); // 정답은 맞춘 횟수를 저장
for(int i=0;i<3;i++)
correct[i] = {0,i+1};
/*
correct[i] = {맞춘 횟수,학생 번호}
correct[0] = {0,1} / correct[1] = {0,2} / correct[2] ={0,3}
*/
for(int i=0;i<answers.size();i++){
if(answers[i] == ans1[i%5]) correct[0].first++;
if(answers[i] == ans2[i%8]) correct[1].first++;
if(answers[i] == ans3[i%10]) correct[2].first++;
}
sort(correct.begin(),correct.end(),Op());
if(correct[0].first!=correct[1].first)
answer.push_back(correct[0].second);
else if(correct[0].first==correct[1].first && correct[1].first!=correct[2].first){
answer.push_back(correct[0].second);
answer.push_back(correct[1].second);
}
else{
answer.push_back(correct[0].second);
answer.push_back(correct[1].second);
answer.push_back(correct[2].second);
}
return answer;
}
다시 이쁘게 작성해보았다. 패턴의 size가 작고 독립적이라고 판단되어 그냥 고정된 배열로 선언해줬다. 너무 어렵게 생각해서 패턴은 뽑는데 생각을 너무 많이한 것 같다.
This post is licensed under CC BY 4.0 by the author.