Problems & Solutions -- 5. divisions in python2 & 3
来源:互联网 发布:淘宝盗图处罚规则2016 编辑:程序博客网 时间:2024/06/07 03:30
总是遇到问题,有些问题很快就能解决,有些问题要拖好久。
以前肯定看到过,python3 中除法和python2 的区别,但是再经历一次,印象更深刻了。
昨天的一道题,明显逻辑、语句都没问题,就是运行结果出了问题。
找了很久,终于发现python3两个整数相除,结果变成了浮点型数据,而且数值还改变了。
python3 中:
(结果科学技术法来表示了,结果保留了16位小数,用int()方法把浮点型数据转化为整型数后,结果明显不对了)
>>> num = 111111111111111111111111112222222222222222222222233333333333333333344444444444444455555555566666666666677777777788888899999999990000000000000000000000>>> num/101.1111111111111112e+148>>> >>> b = num/10>>> int(b)11111111111111111813031060100545474006818973626037027225995557783492768094069335654534362502356440597308689154830054629118849256011654144238332936192>>>
python2中:
(结果最后的L,L for long int, 长整型数据)
>>> num = 111111111111111111111111112222222222222222222222233333333333333333344444444444444455555555566666666666677777777788888899999999990000000000000000000000>>> num/1011111111111111111111111111222222222222222222222223333333333333333334444444444444445555555556666666666667777777778888889999999999000000000000000000000L>>>
遇到问题的算法:
算法学习笔记–6.trailing-zeros
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> 1/11.0>>> 2/12.0>>> def trailingZeros(n): result, i = 1, 1 while i <= n: result = result * i i += 1 answer = 0 while result % 10 == 0: answer += 1 result = result/10 return answer>>> trailingZeros(100)1>>> trailingZeros(105)1>>>
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> 10/101>>> 10055/101005>>> def trailingZeros(n): result, i = 1, 1 while i <= n: result = result * i i += 1 answer = 0 while result % 10 == 0: answer += 1 result = result/10 return answer>>> trailingZeros(10)2>>> trailingZeros(100)24>>> trailingZeros(105)25>>>
阅读全文
0 0
- Problems & Solutions -- 5. divisions in python2 & 3
- Problems & Solutions--parsers in bs4
- Problems & Solutions --3
- Problems & Solutions -- 7 MySQLdb in python
- Problems & Solutions --8 SMTP in flask_mail
- Some common problems in RAD and their solutions/work arounds
- 【转】Cygwin Problems & Solutions
- Cygwin Problems & Solutions 收藏
- U3: Problems and solutions
- Problems & Solutions -- HTTPError: Forbidden
- Problems & Solutions -- 4
- Common Problems (and Their Solutions) for java
- Oracle wait problems and potential solutions
- Stable Problems Get High-Quality Solutions
- Lesson: Common Problems (and Their Solutions)
- Problems & Solutions -- 6. MySql 中文乱码问题
- Booting problems in Solaris
- Restriction problems in eclipse
- 基于拉链法的散列表(c++版)
- JAVA 水仙花数
- 利用数组模拟栈实现简单的表达式中缀转后缀并进行计算
- ImageView宽度填满屏幕,高度自适应
- Count the Colors(区间更新,暴力)
- Problems & Solutions -- 5. divisions in python2 & 3
- xib的屏幕适配问题
- AbstractQueuedSynchronizer类源码解析
- javaseday17(Vector 链表模拟队列或者堆栈 ArrayList 拆装箱 哈希)
- Java的自动装箱与拆箱机制
- unity:C#控制人在真实环境中行走
- 仿美团等APP城市选择
- 个人保存的一些IT资料网站
- H5 照片墙制作