鱼C论坛

 找回密码
 立即注册
查看: 2242|回复: 6

[已解决]时间的进制转换

[复制链接]
发表于 2021-5-3 10:56:46 | 显示全部楼层 |阅读模式

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

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

x
  1.         for index in range(6):
  2.             temp = self.end[index] - self.begin[index]
  3.             # 低位不够减,需要向高位借位
  4.             if temp < 0:
  5.                 # 测试高位是否有得借,没得借的话再向高位借……
  6.                 i = 1
  7.                 while self.lasted[index-i] < 1:
  8.                     self.lasted[index-i] += self.borrow[index-i] - 1 #这里小甲鱼的代码,-1是不是多减了一次?月份只加了11个月,如果2020年1月1日1时 减去 2019年1月2日1时(每月按31天算),那么返回值[年差值,月,日,时,分,秒]不就变成[0,10,30,0,0,0]了吗,10个月30天?不该是11个月30天吗?
  9.                     self.lasted[index-i-1] -= 1
  10.                     i += 1
  11.                 self.lasted.append(self.borrow[index] + temp)
  12.                 self.lasted[index-1] -= 1 #这里已经给月份减一了
  13.             else:
  14.                 self.lasted.append(temp)
复制代码


没搞懂 ,用105秒试了一下,不管有没有这个-1,结果输出都没错,都是1分45秒
最佳答案
2021-5-4 19:39:34
白本羽 发表于 2021-5-3 15:52
我用3601秒试了一下,模拟了跨三个单位且中间不够借位的情况,结果还是对的,,重新编译是指保存 ...

首先程序确实是有问题,
  1. time_begin = [2019,1,2,1,0,0]   #开始
  2. time_end = [2020,1,1,1,0,0]     #结束
  3. time_borrow = [1,12,31,24,60,60]#借位
  4. time_lasted = []     #存放运算结果
  5. for index in range(6):
  6.     temp = time_end[index] - time_begin[index]
  7.     # 低位不够减,需要向高位借位
  8.     if temp < 0:
  9.         # 测试高位是否有得借,没得借的话再向高位借……
  10.         i = 1
  11.         while time_lasted[index - i] < 1:
  12.             time_lasted[index - i] += time_borrow[index - i] - 1
  13.             time_lasted[index - i - 1] -= 1
  14.             i += 1
  15.         time_lasted.append(time_borrow[index] + temp)
  16.         time_lasted[index - 1] -= 1
  17.     else:
  18.         time_lasted.append(temp)

  19. print(time_lasted)
复制代码

用time_begin = [2019,1,2,1,0,0] 和time_end = [2020,1,1,1,0,0]
self.lasted[index-i] += self.borrow[index-i] - 1是这个时
我这边运行结果是[0, 9, 30, 0, 0, 0]
self.lasted[index-i] += self.borrow[index-i]用这个时
结果是[1, 10, 30, 0, 0, 0]
如果你结果一样可能原因时time_begin = [2019,1,2,1,0,0] 和time_end = [2020,1,1,1,0,0] 设置有问题
或者是文件出错需要重新打开一下(可能性比较小)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-3 11:29:21 | 显示全部楼层
self.lasted[index-i] += self.borrow[index-i] - 1
这一段指的是借位列表减一的值赋给lasted列表,去掉-1程序运行结果肯定是不一样的
你如果测试的一样可能是因为修改后没有重新编译运行
而且这种问题你要发布全部代码,只有一个片段,我只能去猜每个列表的作用,看的头痛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-3 15:52:28 | 显示全部楼层
伏惜寒 发表于 2021-5-3 11:29
self.lasted += self.borrow - 1
这一段指的是借位列表减一的值赋给lasted列表,去掉-1程序运行结果肯定是 ...
  1. import time as t


  2. class MyTimer:
  3.     def __init__(self):
  4.         self.unit = ['年', '月', '天', '小时', '分钟', '秒']
  5.         self.borrow = [1, 12, 31, 24, 60, 60]
  6.         self.prompt = "未开始计时!"
  7.         self.lasted = []
  8.         self.begin = 0
  9.         self.end = 0

  10.     # 开始计时
  11.     def start(self):
  12.         self.begin = t.localtime()
  13.         self.prompt = "提示:请先调用stop()结束计时!"
  14.         print("计时开始……")

  15.     # 停止计时
  16.     def stop(self):
  17.         if not self.begin:
  18.             print("提示:请先调用start()开始计时!")
  19.         else:
  20.             self.end = t.localtime()
  21.             self._calc()
  22.             print("计时结束!")

  23.     # 内部方法,计算运行时间
  24.     def _calc(self):
  25.         self.lasted = []
  26.         self.prompt = "总共运行了"
  27.         for index in range(6):
  28.             temp = self.end[index] - self.begin[index]
  29.             # 低位不够减,需要向高位借位
  30.             if temp < 0:
  31.                 # 测试高位是否有得借,没得借的话再向高位借……
  32.                 i = 1
  33.                 while self.lasted[index - i] < 1:
  34.                     self.lasted[index - i] += self.borrow[index - i] - 1
  35.                     self.lasted[index - i - 1] -= 1
  36.                     i += 1
  37.                 self.lasted.append(self.borrow[index] + temp)
  38.                 self.lasted[index - 1] -= 1
  39.             else:
  40.                 self.lasted.append(temp)

  41.         # 由于高位随时会被借位,所以打印要放在最后
  42.         for index in range(6):
  43.             if self.lasted[index]:
  44.                 self.prompt += str(self.lasted[index]) + self.unit[index]

  45.         # 为下一轮计算初始化变量
  46.         self.begin = 0
  47.         self.end = 0
  48.         print(self.prompt)

  49.     # 调用实例直接显示结果
  50.     def __str__(self):
  51.         return self.prompt

  52.     __repr__ = __str__

  53.     # 计算两次计时器对象之和
  54.     def __add__(self, other):
  55.         prompt = "总共运行了"
  56.         result = []
  57.         for index in range(6):
  58.             result.append(self.lasted[index] + other.lasted[index])
  59.             if result[index]:
  60.                 prompt += (str(result[index]) + self.unit[index])
  61.         return prompt


  62. t1 = MyTimer()
  63. t2 = MyTimer()
  64. t1.start()
  65. t.sleep(3661)
  66. t1.stop()
  67. t2.start()
  68. t.sleep(15)
  69. t2.stop()
  70. print(t1 + t2)
复制代码


我用3601秒试了一下,模拟了跨三个单位且中间不够借位的情况,结果还是对的,,重新编译是指保存修改后的代码运行吗?我是分两个不同文件运行的.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-4 19:39:34 | 显示全部楼层    本楼为最佳答案   
白本羽 发表于 2021-5-3 15:52
我用3601秒试了一下,模拟了跨三个单位且中间不够借位的情况,结果还是对的,,重新编译是指保存 ...

首先程序确实是有问题,
  1. time_begin = [2019,1,2,1,0,0]   #开始
  2. time_end = [2020,1,1,1,0,0]     #结束
  3. time_borrow = [1,12,31,24,60,60]#借位
  4. time_lasted = []     #存放运算结果
  5. for index in range(6):
  6.     temp = time_end[index] - time_begin[index]
  7.     # 低位不够减,需要向高位借位
  8.     if temp < 0:
  9.         # 测试高位是否有得借,没得借的话再向高位借……
  10.         i = 1
  11.         while time_lasted[index - i] < 1:
  12.             time_lasted[index - i] += time_borrow[index - i] - 1
  13.             time_lasted[index - i - 1] -= 1
  14.             i += 1
  15.         time_lasted.append(time_borrow[index] + temp)
  16.         time_lasted[index - 1] -= 1
  17.     else:
  18.         time_lasted.append(temp)

  19. print(time_lasted)
复制代码

用time_begin = [2019,1,2,1,0,0] 和time_end = [2020,1,1,1,0,0]
self.lasted[index-i] += self.borrow[index-i] - 1是这个时
我这边运行结果是[0, 9, 30, 0, 0, 0]
self.lasted[index-i] += self.borrow[index-i]用这个时
结果是[1, 10, 30, 0, 0, 0]
如果你结果一样可能原因时time_begin = [2019,1,2,1,0,0] 和time_end = [2020,1,1,1,0,0] 设置有问题
或者是文件出错需要重新打开一下(可能性比较小)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-4 20:06:43 | 显示全部楼层
白本羽 发表于 2021-5-3 15:52
我用3601秒试了一下,模拟了跨三个单位且中间不够借位的情况,结果还是对的,,重新编译是指保存 ...

其次结果不对的原因不在你找到的那个地方,在
  1. #测试
  2. time_begin = [2019,1,2,1,0,0]   #开始
  3. time_end = [2020,1,1,1,0,0]     #结束
  4. time_borrow = [1,12,31,24,60,60]#借位
  5. time_lasted = [0,0,0,0,0,0]     #存放运算结果

  6. def calc():
  7.     for index in range(6):
  8.         temp = time_end[index] - time_begin[index]
  9.         if temp < 1:
  10.             # 测试高位是否有得借,没得借的话再向高位借……
  11.             i = 1
  12.             print(time_lasted[index - i])
  13.             while time_lasted[index - i] < 0:
  14.                 time_lasted[index - i] += time_borrow[index - i] + 1
  15.                 time_lasted[index - i - 1] -= 1
  16.                 i += 1
  17.             time_lasted[index] = time_borrow[index] + temp    #我换成赋值了
  18.             #删掉这段代码结果就是对的time_lasted[index - 1] -= 1
  19.         else:
  20.             time_lasted[index] = temp  #我换成赋值了

  21. calc()
  22. print(time_lasted)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-4 22:55:45 | 显示全部楼层
本帖最后由 白本羽 于 2021-5-4 23:10 编辑
伏惜寒 发表于 2021-5-4 20:06
其次结果不对的原因不在你找到的那个地方,在

  1. 1
  2. 12
  3. 30
  4. 24
  5. 60
  6. [1, 12, 30, 24, 60, 60]
复制代码

那个我运行了你的代码,出来的好像是借位啊..不是结果,而且从一年到第二年不是经过12个月嘛,我这就差了一天,怎么答案会是10个月30天呢?
我改了一下你的代码
  1. time_begin = [2019, 1, 2, 1, 0, 0]  # 开始
  2. time_end = [2020, 1, 1, 1, 0, 0]  # 结束
  3. time_borrow = [1, 12, 31, 24, 60, 60]  # 借位
  4. time_lasted = []  # 存放运算结果
  5. for index in range(6):
  6.     temp = time_end[index] - time_begin[index]
  7.     # 低位不够减,需要向高位借位
  8.     if temp < 1:  #  temp应该是<1的呀
  9.         # 测试高位是否有得借,没得借的话再向高位借……
  10.         i = 1
  11.         while time_lasted[index - i] < 1:
  12.             time_lasted[index - i] += time_borrow[index - i]  #没必要-1
  13.             time_lasted[index - i - 1] -= 1
  14.             i += 1
  15.         time_lasted.append(time_borrow[index] + temp)
  16.         time_lasted[index - 1] -= 1 #这里-1
  17.     else:
  18.         time_lasted.append(temp)

  19. print(time_lasted)
复制代码

结果是
  1. [0, 11, 29, 23, 59, 60]
复制代码

感谢解答,我已经搞懂了 ,不过问题确实是出在我说的那个地方呀.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 01:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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