◎ 문제
○ 출처
https://programmers.co.kr/learn/courses/30/lessons/12901
○ 문제 설명
2016년 1월 1일은 금요일입니다. 2016년 a월 b일은 무슨 요일일까요? 두 수 a ,b를 입력받아 2016년 a월 b일이 무슨 요일인지 리턴하는 함수, solution을 완성하세요. 입니다. 예를 들어 a=5, b=24라면 5월 24일은 화요일이므로 문자열 "TUE"를 반환하세요. |
○ 제한 조건
|
○ 입출력 예
![]() |
○ 작성 코드 예시
public class Solution {
public string solution(int a, int b) {
string answer = "";
return answer;
}
}
◎ 나의 문제 풀이
using System;
public class Solution
{
public string solution(int a, int b)
{
string answer = "";
DateTime date = new DateTime(2016, a, b);
// 무슨 요일인지 알아냄
answer = date.DayOfWeek.ToString();
// 반환된 요일의 맨앞 3글자만 취함
answer = answer.Substring(0, 3);
return answer.ToUpper();
}
}
◎ 다른 문제 풀이
public class Solution {
public string solution(int a, int b) {
string answer = "";
int result = 0;
int year = 2016;
int month = a;
int day = b;
if (month < 3)
{
year--;
month += 12;
}
// 년도를 나누기 위해서 문자열로 형변환
string str_year = year.ToString();
int YA = int.Parse(str_year.Substring(0, 2));
int YB = int.Parse(str_year.Substring(2, 2));
result = day + ((13 * (month + 1)) / 5) + YB + (YB / 4) + (YA / 4) - (2 * YA);
result %= 7;
switch (result)
{
case 0:
answer = "SAT";
break;
case 1:
answer = "SUN";
break;
case 2:
answer = "MON";
break;
case 3:
answer = "TUE";
break;
case 4:
answer = "WED";
break;
case 5:
answer = "THU";
break;
case 6:
answer = "FRI";
break;
}
return answer;
}
}
- 특정 날짜의 요일을 구하는 첼러의 합동식(Zeller’s congruence)을 이용한 풀이이다.
◎ 풀이 참고
<첼러의 합동식> https://terms.naver.com/entry.nhn?docId=3534029&cid=60209&categoryId=60209
- 출처 : 네이버 지식백과 |
'프로그래밍 문제 풀이 > C#' 카테고리의 다른 글
[프로그래밍 문제 풀이] 프로그래머스 - 정수 내림차순으로 배치하기 (C#) (0) | 2020.07.16 |
---|---|
[프로그래밍 문제 풀이] 프로그래머스 - 문자열 내림차순으로 배치하기 (C#) (0) | 2020.07.16 |
[프로그래밍 문제 풀이] 프로그래머스 - 체육복 (C#) (0) | 2020.07.15 |
[프로그래밍 문제 풀이] 프로그래머스 - 짝수와 홀수(C#) (0) | 2020.07.14 |
[프로그래밍 문제 풀이] 프로그래머스 - 평균 구하기(C#) (0) | 2020.07.14 |