1 # 整数部分十进制转二进制
2
3 num = int(raw_input(">>>"))
4
5 if num < 0:
6 isNeg = True
7 num = abs(num)
8 else:
9 isNeg = False
10 result = ''
11 if num == 0:
12 result = ''
13 while num > 0:
14 result = str(num%2) + result
15 num = num/2
16 if isNeg:
17 result = '-' + result
18 # 小数部分十进制转二进制
19
20 x = float(raw_input('Enter a decimal number between 0 and 1: '))
21
22 p = 0
23 while ((2**p)*x)%1 != 0:
24 print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
25 p += 1
26
27 num = int(x*(2**p))
28
29 result = ''
30 if num == 0:
31 result = ''
32 while num > 0:
33 result = str(num%2) + result
34 num = num/2
35
36 for i in range(p - len(result)):
37 result = '' + result
38
39 result = result[0:-p] + '.' + result[-p:]
40 print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
# 穷举法猜测检验平方根
x = 25
epsilon = 0.01
step = epsilon**2
numGuesses = 0
ans = 0.0
while (abs(ans**2 - x)) >= epsilon and ans <= x:
ans += step
numGuesses += 1
print('numGuesses = ' + str(numGuesses))
if abs(ans**2-x) >= epsilon:
print('Failed on square root of ' + str(x))
else:
print(str(ans) + ' is close to the square root of ' + str(x))
# 二分法猜测检验平方根
# bisection search for square root x = 12345
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
numGuesses += 1
if ans**2 < x:
low = ans
else:
high = ans
ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))
# Lecture 3.7, slide 3 # 牛顿-罗斐逊 算法搜寻平方根(g-(g**2-k)/2g) epsilon = 0.01
y = 24.0
guess = y/2.0 while abs(guess*guess - y) >= epsilon:
guess = guess - (((guess**2) - y)/(2*guess))
print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess)) #第一个python程序
import pickle as p linkmanfile = 'linkman.data'
#the name of the file where we will store the object linkman = { 'zhangyunpeng' : '',
'xuleisen' : '',
'yinrui' : '',
'yancangkuo' : '',
'lijizhou' : '',
'liuyulong' : ''
}
#set up linkman data base print '%d lineman:' % len(linkman)
for name, qq in linkman.items():
print '%s : %s' % (name, qq)
#list original listing print'(1-search 2-delete 3-add 0-revise)'
#prompting of operation k = int(raw_input('please input:'))
if k == 1:
s = raw_input('Search the linkman name:')
print '%s' % linkman[s] elif k == 2:
d = raw_input('delete the linkman name:')
del linkman[d] elif k == 3:
a = raw_input('add the linkman name:')
A = raw_input('add the linkman number:')
linkman[a] = A elif k == 0:
r = raw_input('which revise:')
linkman[r] = raw_input('revised number:')
#code of process for name, qq in linkman.items():
print '%s : %s' % (name, qq)
#print new listing f = file(linkmanfile, 'w')
p.dump(linkman, f)
#put data into a file for using next

 

 

最新文章

  1. (3)WebApi客户端调用
  2. Android使用ListView应该注意的地方
  3. Python学习01 Hello World
  4. Git学习笔记——一个NB的分布式版本控制系统
  5. ubuntu14.04.03 vsftpd
  6. MVC中使用EF(2):实现基本的CRUD功能
  7. Qt通过Http上传文件(路过)
  8. [ mysql优化一 ] explain解释select语句
  9. 既然函数也是对象,那么为什么this不指向普通函数?
  10. java struts学习-拦截器
  11. EntityManagerFactory 是多线程的 将其变成一个单线程(使用静态方法)提交效率
  12. git lg 配置
  13. How to Pronounce the Word ARE
  14. Hadoop特点
  15. VS2013新建web项目时出错,系统找不到指定文件
  16. 洛谷P3241 开店
  17. Trove系列(八)——Trove的配置管理相关的功能介绍
  18. Enterprise Library 3.1 参考源码索引
  19. angular学习笔记(二十六)-$http(4)-设置请求超时
  20. VS2010/MFC编程入门之十九(对话框:颜色对话框)

热门文章

  1. thinkphp装修平台源码
  2. “windows的批处理”与“Linux的shell script”的类比学习
  3. 探索未知种族之osg类生物---呼吸分解之事件循环二
  4. AppStore企业账号打包发布APP流程详解
  5. lodash 中常用的方法
  6. JDK8集合类源码解析 - ArrayList
  7. ListView 删除item删除不了的问题解决办法
  8. 使用jackson美化输出json/xml
  9. [cmd]如何设置 Windows 默认命令行窗口大小和缓冲区大小
  10. Mac 环境通用