''' 计算学生在地球上的生活天数 输入一个学生的出生日期,再输入今天的日期,计算今天是这位同学在地球上生活的天数,当然只从出生那天算起,而且包括今天。为了简化程序的复杂度,测试数据假定所有年份的2月份都是 28 天,没有闰年的情况。

输入描述

两行,各有两个标准的日期(年月日),参照样例,分别表示学生出生的日子和今天的日子。

输出描述

一个数,表示该学生在地球上生活的天数。

样例输入 1

2015-9-1 2016-4-3 样例输出 1

215 '''

思路:

  1. 将输入的日期分别提取出年、月、日;
  2. 分别计算出出生日期和今天的日期距离1900年1月1日的天数;
  3. 计算出生日期和今天的日期之间的天数差;
  4. 输出天数差。

代码实现如下:

birth = input().split('-')
today = input().split('-')

birth_year = int(birth[0])
birth_month = int(birth[1])
birth_day = int(birth[2])

today_year = int(today[0])
today_month = int(today[1])
today_day = int(today[2])

days_birth = (birth_year - 1900) * 365 + (birth_month - 1) * 30 + birth_day
days_today = (today_year - 1900) * 365 + (today_month - 1) * 30 + today_day

days = days_today - days_birth

print(days)

复杂度分析:

时间复杂度:O(1),只需要进行一次计算。

空间复杂度:O(1),只需要存储输入的日期和输出的结果。

Python 代码计算学生在地球上的生活天数

原文地址: https://www.cveoy.top/t/topic/pLkb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录