[C++, 프로그래머스 - Lv.0] 주사위 게임 3
사용 언어 : C++
< 문제 내용 >
< 생각 >
해당 문제는 주사위가 나온 값에 따라서 달라지는 점수 공식을 대입하여, 최종적인 점수를 반환하면 됐다.
if문을 통해서 주사위가 나온 결과들을 도출하면 되는데, 다른 방법이 있을 것 같아서 아래의 코드가 나오게 되었다.
switch문 안의 duple.second에 들어가 있는 데이터의 산출 방식은 a, b, c, d가 모두 2로 동일한 숫자를 가지고 있을 때, dupleMap[a ~ d]는 모두 dupleMap[2]를 의미하므로, duple.second는 4가 들어가게 된다.
이러한 성질의 차이를 통해서 swtich로 여러 case가 나뉠 수 있도록 했다.
< 해결 내용 >
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int solution(int a, int b, int c, int d) {
map<int, int> dupleMap;
int p = 0;
int q = 0;
int r = 0;
int answer = 0;
int type = 0;
dupleMap[a]++;
dupleMap[b]++;
dupleMap[c]++;
dupleMap[d]++;
for (auto duple : dupleMap)
{
type = (type > duple.second) ? type : duple.second;
switch (duple.second)
{
case 4:
return 1111 * duple.first;
case 3:
p = duple.first;
break;
case 2:
if (p == 0)
{
p = duple.first;
}
else
{
q = duple.first;
}
break;
case 1:
answer = min({ a,b,c,d });
if (q == 0)
{
q = duple.first;
}
else
{
r = duple.first;
}
break;
}
}
return (type == 3) ? pow((10 * p) + q, 2) : (type == 2) ? (((p + q) * (abs(p - q))) * !(bool)r) + (q * r) : min({ a,b,c,d });
}
'코테 > 프로그래머스' 카테고리의 다른 글
[C++, 프로그래머스 - Lv.0] 연속된 수의 합 (0) | 2023.07.19 |
---|---|
[C++, 프로그래머스 - Lv.0] 안전지대 (0) | 2023.07.19 |
[C++, 프로그래머스 - Lv.0] 겹치는 선분의 길이 (0) | 2023.07.04 |
[C++, 프로그래머스 - Lv.0] 정수를 나선형으로 배치하기 (0) | 2023.07.04 |
[C++, 프로그래머스 - Lv.0] 옹알이 (1) (0) | 2023.07.04 |