프로그래머스

C# - 이모티콘 할인행사

ybbro 2023. 2. 15. 22:40

https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

using System;
using System.Linq;

public class Solution {
    public int[] solution(int[,] users, int[] emoticons)
    {
        // 답은 2개만 반환하므로 길이 2
        int[] answer = new int[2] {0, 0};
        // 2차원 배열은 모든 원소를 길이로 반환하므로 2를 나누어주면 이용자 수
        int users_Length = users.Length / 2;

        // 각 이모티콘에 대해 10, 20, 30, 40% 할인 가능 
        // -> 따라서 할인할 수 있는 경우의 수는 4^이모티콘의 수
        int discount_case = (int)Math.Pow(4, emoticons.Length);

        // 각 이모티콘의 할인율을 저장할 배열(모두 10% 할인인 상태로 시작)
        int[] emoticons_discount_rate = Enumerable.Repeat(10, emoticons.Length).ToArray();
        // 루프 시작부터 첫 항목에 10을 더하고 시작하므로 첫 항목만 0
        emoticons_discount_rate[0] = 0;

        // 각 이모티콘의 할인가를 저장할 배열
        int[] emoticons_discounted = new int[emoticons.Length];

        // 모든 할인율 경우의 수만큼 시행
        for (int i = 0; i < discount_case; i++)
        {
            // 1) 할인율 변경
            emoticons_discount_rate[0] += 10;
            for (int j = 0; j < emoticons_discount_rate.Length; j++)
            {
                // 40% 할인까지만 있으므로 50% 할인이 되면
                if (emoticons_discount_rate[j] > 40)
                {
                    // 해당 이모티콘 할인율을 10%로 초기화하고
                    emoticons_discount_rate[j] = 10;
                    //다음 항목 할인율을 10% 높임 
                    emoticons_discount_rate[j + 1] += 10;
                }
                // 할인율이 50%가 되지 않는 항목이 처음 나왔을 때 루프를 종료
                else
                    break;
            }

            // 2) 변경된 할인율에 맞게 할인가 산출
            for (int j = 0; j < emoticons.Length; j++)
            {
                // 가격은 100 단위로 올라가므로 할인율을 적용하더라도 정수
                emoticons_discounted[j] = (int)(((double)(100 - emoticons_discount_rate[j]) * emoticons[j] / 100));
            }

            // 3) 이모티콘 플러스 가입 유저수, 이모티콘 매출 합을 산출
            int emoticon_plus_join = 0;
            int emoticon_sell_price = 0;
            // 각 유저에 대해
            for (int j = 0; j < users_Length; j++)
            {
                // 해당 유저가 구매할 이모티콘 가격 합산용 변수
                int cart_price = 0;
                // 각 항목 할인율과 비교
                for (int k = 0; k < emoticons_discount_rate.Length; k++)
                {
                    // 해당 이모티콘의 할인율이 구매 할인율 이상이면
                    if (users[j, 0] <= emoticons_discount_rate[k])
                        // 할인가를 카트에 추가
                        cart_price += emoticons_discounted[k];
                }
                // 구입할 이모티콘의 가격의 합이 유저가 구매 의사가 있는 가격 미만이면
                if (users[j, 1] > cart_price)
                    // 해당 유저는 이모티콘 구매 -> 구매 가격을 가산
                    emoticon_sell_price += cart_price;
                // 구매 의사가 있는 가격 이상이면
                else
                    // 해당 유저는 이모티콘 플러스 가입
                    emoticon_plus_join++;
            }

            // 4) 기존의 해와 비교
            // 이모티콘 플러스 가입 유저가 더 많은 경우
            if (emoticon_plus_join > answer[0])
            {
                // 서비스 가입 유저가 많은 쪽이 답
                answer[0] = emoticon_plus_join;
                answer[1] = emoticon_sell_price;
            }
            // 이모티콘 플러스 가입 유저가 동일한 경우
            else if (emoticon_plus_join == answer[0])
            {
                // 이모티콘 판매가가 높은 쪽이 답
                if(answer[1] < emoticon_sell_price)
                    answer[1] = emoticon_sell_price;
            }
        }
        return answer;
    }
}