鱼C论坛

 找回密码
 立即注册
查看: 3104|回复: 44

[技术交流] Python:每日一题 163

[复制链接]
发表于 2018-3-12 16:42:56 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 冬雪雪冬 于 2018-3-14 20:50 编辑

我们的玩法做了一下改变:

1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。

题目:
本次的题目难度也不大,我们知道python整数的除法不论是否能够整除,得到的都是浮点数,现在生成一个新的整数类,并且其除法“/”运算当结果为整数时得到整数,否则得到浮点数。
例如:
  1. >>> a = Nint(9)
  2. >>> b = Nint(8)
  3. >>> c = Nint(4)
  4. >>> a / c
  5. 2.25
  6. >>> b / c
  7. 2
  8. >>> 12 / c
  9. 3
  10. >>> 13 / c
  11. 3.25
  12. >>> a / 3
  13. 3
  14. >>> a / 5
  15. 1.8
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-12 16:55:58 | 显示全部楼层
本帖最后由 Chase_Kas 于 2018-3-26 23:06 编辑

学完类了!现在来答一下~~
  1. class Nint(int):

  2.     def __truediv__(self, other):
  3.         result = int.__truediv__(self, other)
  4.         if int(result) == result:
  5.             return int(int.__truediv__(self, other))
  6.         else:
  7.             return int.__truediv__(self, other)

  8.     def __rtruediv__(self, other):
  9.         result = int.__rtruediv__(self, other)
  10.         if int(result) == result:
  11.             return int(int.__truediv__(other, self))
  12.         else:
  13.             return int.__truediv__(other, self)
复制代码

看了其他人的发现右除可以直接调用上边的除!!哇,脑袋是个好东西,可惜我没有哦
  1. class Nint(int):

  2.     def __truediv__(self, other):
  3.         result = int.__truediv__(self, other)
  4.         if int(result) == result:
  5.             return int(int.__truediv__(self, other))
  6.         else:
  7.             return int.__truediv__(self, other)

  8.     def __rtruediv__(self, other):
  9.         return Nint.__truediv__(other, self)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 17:04:33 | 显示全部楼层
  1.  # 继承int类,+ - *依旧可以使用,重写/
  2. class Nint(int):
  3.     def __init__(self, number):
  4.         self.number = number
  5.         
  6.     def __truediv__(self, value):
  7.         if isinstance(value, int):
  8.             value = Nint(value)
  9.         a = self.number/value.number
  10.         if a == int(a):
  11.             return self.number//value.number
  12.         else:
  13.             return self.number/value.number
  14.     def __rtruediv__(self, value):
  15.         if isinstance(value, int):
  16.             value = Nint(value)
  17.         a = value.number/self.number
  18.         if a == int(a):
  19.             return value.number//self.number
  20.         else:
  21.             return value.number/self.number


  22. a = Nint(9)
  23. b = Nint(8)
  24. c = Nint(4)

  25. print(a/c)
  26. print(b/c)
  27. print(12/c)
  28. print(13/c)
  29. print(a/3)
  30. print(a/5)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 17:05:59 | 显示全部楼层
  1. class Nint(int):
  2.         def __truediv__(self,other):
  3.                 if int(int(self) / int(other)) == int(self) / int(other):
  4.                         return int(int(self) / int(other))
  5.                 else:
  6.                         return int(self) / int(other)
  7.         def __rtruediv__(other,self):
  8.                 if int(int(self) / int(other)) == int(self) / int(other):
  9.                         return int(int(self) / int(other))
  10.                 else:
  11.                         return int(self) / int(other)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 17:08:27 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self, other):
  3.         if int.__truediv__(self, other) == int(int.__truediv__(self, other)):
  4.             return int(int.__truediv__(self, other))
  5.         else:
  6.             return int.__truediv__(self, other)
  7.     def __rtruediv__(self, other):
  8.         return Nint.__truediv__(other, self)


  9. a = Nint(9)
  10. b = Nint(8)
  11. c = Nint(4)

  12. print(a/b)
  13. print(a/c)
  14. print(b/c)
  15. print(12/c)
  16. print(13/c)
  17. print(a/3)
  18. print(a/5)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 17:17:30 | 显示全部楼层
def Nint(a,b):
    if(a%b == 0):
        return a//b
    else:
        return a/b

print(Nint(9,3))
print(Nint(9,4))

3
2.25

点评

不是写函数是写类  发表于 2018-3-15 21:38
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-3-12 17:45:47 | 显示全部楼层
很快的就写出来啦。。。
  1. class Nint(int):
  2.     def __init__(self,value=1):
  3.         self.value = value

  4.     def __truediv__(self,other):
  5.         k1 = super().__truediv__(other)
  6.         k2 = super().__floordiv__(other)
  7.         if k1 == k2 :
  8.             return int(k1)
  9.         else:
  10.             return k1
  11.     def __rtruediv__(self,other):
  12.         k1 = super().__rtruediv__(other)
  13.         k2 = super().__rfloordiv__(other)
  14.         if k1 == k2 :
  15.             return int(k1)
  16.         else:
  17.             return k1

  18. a=Nint(9)
  19. b=Nint(8)
  20. c=Nint(4)

  21. >>> a/c
  22. 2.25
  23. >>> b/c
  24. 2
  25. >>> 12/c
  26. 3
  27. >>> 13/c
  28. 3.25
  29. >>> a/3
  30. 3
  31. >>> a/5
  32. 1.8
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 18:18:09 | 显示全部楼层
class Inte:
    def __init__(self,x):
        self.num = int(x)
        
    def __truediv__(self,other):
        if self.num % other.getnum() == 0:
            return int(self.num/other.getnum())
        else:
            return round(self.num/other.getnum(),2)
        
    def getnum(self):
        return self.num
        
a = Inte(12)
b = Inte(11)

a/b

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 19:16:31 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,other):
  3.         a=int.__truediv__(self,other)
  4.         if a==int(a):
  5.             return int(a)
  6.         else:
  7.             return a
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 19:43:01 | 显示全部楼层
本帖最后由 溯影 于 2018-3-12 19:57 编辑

sorry sorry上个理解错了,是要写一个类Nint
class Nint:
        def __init__(self,number):
                self.number = number

        def __truediv__(self,other):
                if self.number % other.number == 0:
                        return self.number // other.number
                else:
                        return self.number / other.number
        def __rtruediv__(self,other):
                if self.number % other.number == 0:
                        return self.number // other.number
                else:
                        return self.number / other.number


if __name__ == '__main__':
        a = Nint(12)
        b = Nint(4)
        c = Nint(5)
        print(a/b)
        print(a/c)
        print(b/c)
       
运行结果:
3
2.4
0.8

***Repl Closed***

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 20:02:14 | 显示全部楼层
本帖最后由 luokaoge 于 2018-3-12 20:34 编辑

class Division():
    def __init__(self,a,b):
        c = a/b
        d = int(c)
        if d == c :
            e = int(c)
            c = e
            print(c)
            print(type(c))
        else :
            e = float(c)
            c = e
            print(c)
            print(type(c))

还有除数为0的时候。。。

class Division():
        def __init__(self,a,b):
                if b == 0:
                        print('除数不能为0')
                else:
                        c = a/b
                        d = int(c)
                        if d == c :
                            e = int(c)
                            c = e
                            print(c)
                            print(type(c))
                        else :
                            e = float(c)
                            c = e
                            print(c)
                            print(type(c))

点评

要用除法的魔法方法  发表于 2018-3-15 21:43
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 20:10:50 | 显示全部楼层
class Nint(int):

    def __truediv__(self, other):
        if int.__mod__(self,other) == 0:
            return int(int.__truediv__(self,other))
        else:
            return float(int.__truediv__(self,other))

    def __rtruediv__(self, other):
        if int.__mod__(self,other) == 0:
            return int(int.__rtruediv__(self,other))
        else:
            return float(int.__rtruediv__(self,other))

点评

右除有误  发表于 2018-3-15 21:45

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 20:20:03 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,other):
  3.         if self%other != 0:
  4.             return int.__truediv__(self,other)
  5.         else:
  6.             return int.__floordiv__(self,other)
  7.     def __rtruediv__(self,other):
  8.         if other%self != 0:
  9.             return int.__truediv__(other,self)
  10.         else:
  11.             return int.__floordiv__(other,self)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 20:58:13 | 显示全部楼层
本帖最后由 wyp02033 于 2018-3-12 21:17 编辑
  1. class Nint(int):
  2.     def __truediv__(self, other):
  3.         if self % other:
  4.             return int.__truediv__(self, other)
  5.         else:
  6.             return int.__floordiv__(self, other)

  7.     def __rtruediv__(self, other):
  8.         if other % self:
  9.             return int.__truediv__(other, self)
  10.         else:
  11.             return int.__floordiv__(other, self)
  12. a = Nint(9)
  13. b = Nint(4)
  14. c = Nint(2)

  15. print(a / b)
  16. print(b / c)
  17. print(12 / a)
  18. print(12 / b)
复制代码


结果:
2.25
2
1.3333333333333333
3

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 21:15:05 | 显示全部楼层
class Nint(int):

    def __truediv__(self,other):
        
        if int.__truediv__(self,other) == int(int.__truediv__(self,other)):
            return int(int.__truediv__(self,other))

        else:
            return int.__truediv__(self,other)

  

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 21:34:53 | 显示全部楼层
本帖最后由 LargeCat 于 2018-3-12 21:35 编辑
  1. class Nint(int):
  2.     def __init__(self,x):
  3.        self.x = x
  4.     def __truediv__(self,other):
  5.         if self.x % other == 0:
  6.             return int(self.x/other)
  7.         else:
  8.             return float(self.x/other)

  9. print(Nint(4)/2)
  10. print(Nint(5)/2)
  11. print(9/3)
复制代码


结果
  1. 2
  2. 2.5
  3. 3.0
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 22:28:10 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,value):
  3.         if self % value == 0:
  4.             return int.__floordiv__(self,value)
  5.         else:
  6.             return int.__truediv__(self,value)

  7.     def __rtruediv__(self,value):
  8.         if value % self == 0:
  9.             return int.__floordiv__(value,self)
  10.         else:
  11.             return int.__truediv__(value,self)

  12. a = Nint(9)
  13. b = Nint(8)
  14. c = Nint(4)

  15. print(a / c)
  16. print(b / c)
  17. print(12 / c)
  18. print(13 / c)
  19. print(a / 3)
  20. print(a / 5)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 22:52:31 | 显示全部楼层
class nint:
    def __init__(self,m):
        self.x=m
    def __truediv__(self,other):
        if self.x%other.x==0:
            return int(self.x/other.x)
        else:
            return self.x/other.x

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 23:24:52 | 显示全部楼层
class Nint(int):
    def __init__(self, x):
        self.x = x

    def __truediv__(self, other):
        self.div = int.__truediv__(self, other)
        if self.div == int(self.div):
            return int(self.div)
        else:
            return self.div

    def __rtruediv__(self, other):
        self.div = int.__rtruediv__(self, other)
        if self.div == int(self.div):
            return int(self.div)
        else:
            return self.div

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-12 23:32:07 | 显示全部楼层
  1. class Nint(int):
  2.     def __truediv__(self,value):
  3.         return int.__truediv__(self,value)  if self%value else int.__floordiv__(self,value)
  4.     def __rtruediv__(self,value):
  5.         return int.__rtruediv__(self,value) if value%self else int.__rfloordiv__(self,value)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 15:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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