鱼C论坛

 找回密码
 立即注册
查看: 2432|回复: 2

[已解决]解决问题

[复制链接]
发表于 2023-12-7 15:41:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问这一题怎么解决?
9.2. Exercise
Calling time (NULL) gives you a large integer. Use this value to calculate today's year, date, hours, and minutes.
  Simplifying assumption:we pretend that each year has exactly365 days. The 15th of March is therefore day 83 in year 2011. (use that date to figure out today's "day number")(hint: how many seconds are there in a day?)
You cannot use struct for this assignment.
You cannot use any time-based functions from the standard library such asasctime or strftime.
  You must use one function to calculate the year and day(pass inthe total seconds, and a reference to the year and day).
  You must use a separate function to calculate the hours andminutes.(again, pass the total seconds, and a reference to the hours and minutes)
  You must use a separate function to print the time; this functioncan only take as arguments the year, day, hour, and minutes.
(optional: handle leap-years correctly, and print the month as well).
最佳答案
2023-12-7 15:46:47
这是一个关于通过给定的时间戳来计算年份、日期、小时和分钟的问题。下面是一个可能的解决方案:
  1. def calculate_year_day(total_seconds, year, day):
  2.     seconds_per_day = 24 * 60 * 60
  3.     years = total_seconds // (seconds_per_day * 365)
  4.     days = (total_seconds % (seconds_per_day * 365)) // seconds_per_day
  5.     year += years
  6.     day += days
  7. def calculate_hours_minutes(total_seconds, hours, minutes):
  8.     seconds_per_hour = 60 * 60
  9.     hours = (total_seconds % (24 * 60 * 60)) // seconds_per_hour
  10.     minutes = (total_seconds % (60 * 60)) // 60
  11. def print_time(year, day, hours, minutes):
  12.     print("Today's date is: Year -", year, "Day -", day)
  13.     print("Current time is:", hours, ":", minutes)
  14. # 获取当前时间戳
  15. current_time = time.time()
  16. # 假设给定时间是2011年3月15日,求出今天的“天数”
  17. given_time = time.mktime((2011, 3, 15, 0, 0, 0, 0, 0, 0))
  18. day_number = (current_time - given_time) / (24 * 60 * 60)
  19. # 初始化年份、日期、小时和分钟的变量
  20. year = 2011
  21. day = 83
  22. hours = 0
  23. minutes = 0
  24. # 调用函数来计算年份、日期、小时和分钟
  25. calculate_year_day(current_time, year, day)
  26. calculate_hours_minutes(current_time, hours, minutes)
  27. # 调用函数来打印时间
  28. print_time(year, day, hours, minutes)
复制代码

这个解决方案中,我们使用了三个函数来处理不同的计算和打印任务。首先,我们使用给定时间和当前时间戳来计算今天的“天数”。然后,我们使用计算得到的“天数”来更新年份和日期。接下来,我们使用当前时间戳来计算小时和分钟。最后,我们使用打印函数来输出计算得到的时间。请注意,这只是一个简化的解决方案,没有考虑闰年和月份。如果你想要处理闰年和打印月份,你可以在相关的函数中加入更多的逻辑。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-2 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表