제4문제

4823 단어
제목: 어느 해 어느 달 어느 날을 입력하여 이 날이 이 해의 며칠째라고 판단합니까?
자신의 코드는 다음과 같습니다.
 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Fri Oct  4 22:08:42 2019
 4 
 5 @author: Franz
 6 """
 7 
 8 def leap(year):
 9     index = False
10     if year % 400 == 0:
11         index = True
12     else:
13         if year % 4 == 0 and year % 100 !=0:
14             index = True
15     return index
16 
17 year = int(input('please input the year you want to check: '));
18 month = int(input('please input the month you want to check: '));
19 day = int(input('please input the day you want to check: '))
20 
21 mon_days = [0,31,59,90,120,151,181,212,243,273,304,334];
22 if not leap(year):
23     days = mon_days[month-1]+day;
24 else:
25     if month >= 2:
26         days = month_days[month-1]+1+day
27     else:
28         days = mon_days[month-1]+day;
29 
30 print('it is the %dth day.' % days)

분석: 코드가 엉망으로 쓰여졌는데 그 중에서 많은 조건 판단은if문장에 합쳐서 판단할 수 있고 코드의 우선순위에 대해 고려하지 않았다.윤년월과 비윤년월의 일수 차이를 깨닫고 한 수조에 따라 월별 조회를 하는 것이 장점이다.
다음은 표준 답안(python-2.7x)입니다.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
year = int(raw_input('year:
')) month = int(raw_input('month:
')) day = int(raw_input('day:
')) months = (0,31,59,90,120,151,181,212,243,273,304,334) if 0 < month <= 12: sum = months[month - 1] else: print 'data error' sum += day leap = 0 if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): leap = 1 if (leap == 1) and (month > 2): sum += 1 print 'it is the %dth day.' % sum

좋은 웹페이지 즐겨찾기