鱼C论坛

 找回密码
 立即注册
查看: 341|回复: 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官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-7 16:48:14 | 显示全部楼层
本帖最后由 jackz007 于 2023-12-7 17:00 编辑
  1. #include <stdio.h>
  2. #include <time.h>

  3. int leapyear(int year)
  4. {
  5.         return (! (year % 400) || (! (year % 4) && (year % 100))) ? 1 : 0          ;
  6. }

  7. int mdays(int year , int moonth)
  8. {
  9.         int e[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31}      ;
  10.         return (leapyear(year) && moonth == 2) ? e[moonth - 1] + 1 : e[moonth - 1] ;
  11. }

  12. int ydays(int year)
  13. {
  14.         return (leapyear(year)) ? 366 : 365                                        ;
  15. }

  16. int main(void)
  17. {
  18.         time_t ds , ts , tk                               ;
  19.         int year , moonth , day , hour , minute , second  ;
  20.         time(& ts)                                        ; // ts 为从格林威治时间 1970年1月1日0时0分0秒起算,到当前所经历的总秒数
  21.         ts += 3600 * 8                                    ; // 东8区时差(单位:秒),如果不加,算出来的日期将是格林威治标准时间
  22.         ds = ts / (3600 * 24)                             ; // ds 为通过 ts 计算的从 1970年1月1日起,到当前所经历的总天数
  23.         tk = ts % (3600 * 24)                             ; // tk 为 ts 去除天数后,所剩余不足 1 天的零头秒数
  24.         year = 1970                                       ;
  25.         while(ds >= ydays(year)) {
  26.                 ds -= ydays(year)                         ;
  27.                 year += 1                                 ;
  28.         }
  29.         moonth = 1                                        ;
  30.         while(ds >= mdays(year , moonth - 1)) {
  31.                 moonth ++                                 ;
  32.                 ds -= mdays(year , moonth - 1)            ;
  33.         }
  34.         day = ds + 1                                      ;
  35.         hour = tk / 3600                                  ;
  36.         minute = tk % 3600 / 60                           ;
  37.         second = tk % 3600 % 60                           ;
  38.         printf("%d-%02d-%02d %02d:%02d:%02d\n" , year , moonth , day , hour , minute , second) ;
  39. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-14 03:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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