鱼C论坛

 找回密码
 立即注册
查看: 1305|回复: 5

44讲课后题动动手0,求大佬指教!!!

[复制链接]
发表于 2019-8-31 11:23:14 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class MyTimer:
    def __init__(self):
        self.unit = ['年', '月', '天', '小时', '分钟', '秒']
        self.borrow = [0, 12, 31, 24, 60, 60]
        self.prompt = "未开始计时!"
        self.lasted = []
        self.begin = 0
        self.end = 0
   
    def __str__(self):
        return self.prompt

    __repr__ = __str__

    def __add__(self, other):
        prompt = "总共运行了"
        result = []
        for index in range(6):
            result.append(self.lasted[index] + other.lasted[index])
            if result[index]:
                prompt += (str(result[index]) + self.unit[index])
        return prompt
   
    # 开始计时
    def start(self):
        self.begin = t.localtime()
        self.prompt = "提示:请先调用 stop() 停止计时!"
        print("计时开始...")

    # 停止计时
    def stop(self):
        if not self.begin:
            print("提示:请先调用 start() 进行计时!")
        else:
            self.end = t.localtime()
            self._calc()
            print("计时结束!")

    # 内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = "总共运行了"
        for index in range(6):
            temp = self.end[index] - self.begin[index]

            # 低位不够减,需向高位借位
            if temp < 0:
                # 测试高位是否有得“借”,没得借的话向再高位借......
                i = 1
                while self.lasted[index-i] < 1:
                    self.lasted[index-i] += self.borrow[index-i] - 1
                    self.lasted[index-i-1] -= 1
                    i += 1
               
                self.lasted.append(self.borrow[index] + temp)
                self.lasted[index-1] -= 1
                   #这一段代码求解释,越详细越好,感谢大佬指教!!!
            else:
                self.lasted.append(temp)

        # 由于高位随时会被借位,所以打印要放在最后
        for index in range(6):
            if self.lasted[index]:
                self.prompt += str(self.lasted[index]) + self.unit[index]
        
        # 为下一轮计时初始化变量
        self.begin = 0
        self.end = 0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-31 13:00:44 | 显示全部楼层
那一段红色的里面,while负责往前借位,while的第一行向高位借了borrow数量的数值,第二行把高位减去1(因为高位被借走1)

i += 1 配合整个while的条件就是持续寻找直到找到可以借位的那一位

while后面的两段是这样的,如果前一位刚好能借位,那么就不会运行while而直接向前一位借位,就是说不会再往前寻找

希望把事情说清了...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-31 16:40:34 | 显示全部楼层
facevoid 发表于 2019-8-31 13:00
那一段红色的里面,while负责往前借位,while的第一行向高位借了borrow数量的数值,第二行把高位减去1(因 ...

(1)self.lasted = []
如果开始计时的时间是(2022年2月22日16:30:30),停止时间是(2025年1月23日15:30:30)
index = 1 时 ,temp就小于0,执行while语句
那么self.lasted[index -  i]  变为self.lasted[0]
但是self.lasted 为空列表啊,不能够进行索引啊
(2)while self.lasted[index - i] < 1  (self.lasted还是空列表啊)
是哪里还没理解,请指教啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-1 10:45:20 | 显示全部楼层
kevinspacey 发表于 2019-8-31 16:40
(1)self.lasted = []
如果开始计时的时间是(2022年2月22日16:30:30),停止时间是(2025年1月23日15: ...

对于第一个问题,index=1的时候确实temp<0,会执行while,但是在这之前,index=0的那一轮是没有执行while的,就是说index=0时往self.lasted里面放了一个数据,所以self.lasted不会为空。
第二个问题应该跟第一个一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-9 21:07:01 | 显示全部楼层
  1.     # 内部方法,计算运行时间
  2.     def _calc(self):
  3.         self.lasted = []
  4.         self.prompt = "总共运行了"
  5.         for index in range(6):
  6.             temp = self.end[index] - self.begin[index]
  7.             
  8.             # 低位不够减,需要向高位借位
  9.             if temp < 0:
  10.                 # 测试高位是否有得借,没得借的话再向高位借……
  11.                 i = 1
  12.                 # 测试高位是否有得借,高位不为0,就说明有的借;为0没得借的话,还得向更高位借……
  13.                 while self.lasted[index-i] == 0:
  14.                     # 当前位没得借,还得向更高位借,先改写当前位的值为换算进制减一(没有也要借^_^)
  15.                     self.lasted[index-i] = self.borrow[index-i] - 1
  16.                     # i 继续定位下个高位,直到下个高位不为0
  17.                     i += 1
  18.                 # 之前的负值 加上 换算进制 转正
  19.                 self.lasted.append(self.borrow[index] + temp)
  20.                 # 将定位的高位值减一,借的就是Ta的
  21.                 self.lasted[index-i] -= 1
  22.             else:
  23.                 self.lasted.append(temp)

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

  28.         # 为下一轮计算初始化变量
  29.         self.begin = 0
  30.         self.end = 0
复制代码

测试时答案有问题,个人修改如上(附带说明)
测试代码如下:
  1. borrow = [1, 12, 31, 24, 60, 60]
  2. begin = [2022,2,22,16,30,30]
  3. end = [2025,1,23,15,30,30]
  4. lasted = []
  5. for index in range(6):
  6.     temp = end[index] - begin[index]
  7.     print(f'{index}\t{temp}')
  8.     # 低位不够减,需要向高位借位
  9.     if temp < 0:
  10.         # 测试高位是否有得借,没得借的话再向高位借……
  11.         i = 1
  12.         while lasted[index-i] == 0:
  13.             lasted[index-i] = borrow[index-i] - 1
  14.             i += 1
  15.         lasted[index-i] -= 1
  16.         lasted.append(borrow[index] + temp)
  17.     else:
  18.         lasted.append(temp)
  19.         
  20. print(begin)
  21. print(end)
  22. print(lasted)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-25 11:41:55 | 显示全部楼层
客官这边请 发表于 2019-11-9 21:07
测试时答案有问题,个人修改如上(附带说明)
测试代码如下:

是的 感谢!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 06:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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