C# 알고리즘 코드카타

71. 개인정보 수집 유효기간

잼잼재미 2024. 1. 25. 10:04

 

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(string today, string[] terms, string[] privacies) {
        int[] answer = new int[] { };
        List<int> list = new List<int>();
        int day;
        int month;
        int year;
        int todayDay;
        int todayMonth;
        int todayYear;
        string[] words;

        words = today.Split('.');
        todayDay = int.Parse(words[2]);
        todayMonth = int.Parse(words[1]);
        todayYear = int.Parse(words[0]);

        for (int i = 0; i < privacies.Length; i++)
        {
            words = privacies[i].Split('.');
            words[2] = words[2].Substring(0, 2);
            day = int.Parse(words[2]);
            month = int.Parse(words[1]);
            year = int.Parse(words[0]);

            for (int j = 0; j < terms.Length; j++)
            {
                if (privacies[i][11] == terms[j][0])
                {
                    string str = terms[j].Substring(2);
                    int addMonth = int.Parse(str);

                    month += addMonth;
                    day -= 1;
                    if (day == 0)
                    {
                        day = 28;
                        month -= 1;
                    }
                    while (month > 12)
                    {
                        month -= 12;
                        year++;
                    }

                    break;
                }
            }

            if(todayYear > year)
            {
                list.Add(i + 1);
                continue;
            }
            else if(todayYear == year)
            {
                if(todayMonth > month)
                {
                    list.Add(i + 1);
                    continue;
                }
                else if(todayMonth == month)
                {
                    if(todayDay > day)
                    {
                        list.Add(i + 1);
                        continue;
                    }
                }
            }

        }

        answer = list.ToArray();

        return answer;
    }
}

 

string의 Split과 Substring으로 문자열을 나눠서 int로 형변환 후, 날짜를 비교. 

'C# 알고리즘 코드카타' 카테고리의 다른 글

73. 공원 산책  (0) 2024.02.02
72. 달리기 경주  (0) 2024.01.29
70. 바탕화면 정리  (0) 2024.01.23
69. 성격 유형 검사하기  (0) 2024.01.19
68. 햄버거 만들기  (0) 2024.01.16