from math import sqrt
for a in range(100000):
m = sqrt(a+100)
n = sqrt(a+268)
if m%1==0 and n%1==0:
print(a)
-
输入某年某月某日,判断这一天是这一年的第几天?
思路:先判断是否为闰年,这关系到 2 月份的天数。之后再根据月份值把前几个月的天数累积加起来,最后再加上个“日”,就可以了。
传统方法:
'''输入某年某月某日,判断这一天是这一年的第几天?'''
date = input('请输入日期(格式如:2016-06-13):')
#2016-03-23
#切片从0开始,左包右不包
y = int(date[:4])
m = int(date[5:7])
d = int(date[-2:])#始:终:步长
#判断是否为瑞年
if y%4 == 0 and y%100 != 0 or y%400 == 0 :
m_2 = 29
else:
m_2 = 28
m_s = [0,31,m_2,31,30,31,30,31,31,30,31,30,31]
# print(m_s)
base_sum_day = 0
for i in range(0,m):
# print(m_s[i])
base_sum_day += m_s[i]
# print(base_sum_day)
sum_days = base_sum_day + d
print('{}是该年份的第{}天'.format(date,sum_days))
python模块法:
import datetime
date = input('请输入日期(格式如:2016-06-13):')
# date = '2016-01-23'
#切片从0开始,左包右不包
y = int(date[:4])
m = int(date[5:7])
d = int(date[-2:])#始:终:步长
target_time = datetime.date(y,m,d)
days_count = target_time - datetime.date(target_time.year-1,12,31)
# print(days_count.days)
print('{}是{}年的第{}天'.format(target_time,target_time.year,days_count.days))
# 这个题目主要是date.time模块的使用
网友评论