주사위 게임2

Subin·2024년 7월 4일

Algorithm

목록 보기
7/69

[내 풀이]

#include <string>
#include <vector>

using namespace std;

int solution(int a, int b, int c) {
    int answer = 0;

    if(a!=b && b!=c && c!=a)
        answer = a+b+c;
    else if(a!=b || b!=c || c!=a)
        answer = (a+b+c)*(a*a + b*b + c*c);
    else if(a==b && b==c)
        answer = (a+b+c)*(a*a + b*b + c*c)*(a*a*a + b*b*b + c*c*c);

    return answer;
}

[다른 사람 풀이]

#include <string>
#include <vector>
#include <math.h>
#include <set>

using namespace std;

int solution(int a, int b, int c)
{
    set<int> s{a, b, c};
    if(s.size() == 3)
        return a + b + c;
    if(s.size() == 2)
        return (a + b + c) * (pow(a, 2) + pow(b, 2) + pow(c, 2));
    if(s.size() == 1)
        return (a + b + c) * (pow(a, 2) + pow(b, 2) + pow(c,2)) * (pow(a, 3) + pow(b, 3) + pow(c, 3));

}

math.h와 set을 사용해서
a, b, c를 값으로 갖는 set을 만들어 활용.

set

이진트리 이용
숫자든 문자든 입력값의 중복을 없앤다.
삽입 순서에 상관없이 정렬해서 입력된다.

(예제)

#include <iostream>
#include <set>

using namespace std;

int main(void) {

    set<int> s;

    s.insert(1);
    s.insert(200);
    s.insert(-1);
    s.insert(3);

    int arr[] = { 1, 2, 3, 4, 5, 6 };
    set<int> s1(s.begin(), s.end());    //output : s1 = -1, 1, 3, 200
    
    set<int> s2(arr, arr + 6);        //output : s2 = 1, 2, 3, 4, 5, 6
    
    set<int> s3(s1);        //output : s3 = -1, 1, 3, 200
    
    set<int> s4 = s2;        //output : s4 = 1, 2, 3, 4, 5, 6
    

    return 0;
}
​
  • set<자료형> 변수 기본적인 선언방법

  • set<자료형> 변수(복사할 변수) 선언 후 복사한 값으로 초기화

  • set<자료형> 변수 = 복사할 변수 서언 후 복사한 값으로 초기화


    참고자료
    https://hwan-shell.tistory.com/130

profile
성장하며 꿈꾸는 삶을 살아가고 있는 대학생입니다😊

0개의 댓글