标签:
剧情提要:正剧开始:
(续前节)
星历2016年04月19日 09:02:47, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[例题]。
<span style="font-size:18px;">#例1 def prime(x): if x < 2: return False; for i in range(2, math.ceil(x**0.5)+1): if (i != x) and (x%i==0): return False; return True; #例2 def fun(x): return x**2-2; def bisection(xMin, xMax): if fun(xMin)*fun(xMax) > 0: return float('inf'); elif fun(xMin) == 0: return xMin; elif fun(xMax) == 0: return xMax; else: epsilon = 10e-6; while (abs(xMin-xMax)> epsilon): xMid = (xMin+xMax)/2; if (fun(xMid)*fun(xMin)< 0): xMax = xMid; elif (fun(xMid)*fun(xMax) < 0): xMin = xMid; else: return xMid; return xMid; #例3 def HQFormula(a, b, c): p = (a+b+c)/2; S = math.sqrt(p*(p-a)*(p-b)*(p-c)); return S; #例4 def validTriangle(a, b, c): if (a <= 0 or b <= 0 or c <= 0): return False; array = sorted([a, b, c]); if (array[-1] >= array[0] + array[1]): return False; return True; #例5 def quadratic(a, b, c): if a == 0 and b == 0: if c != 0: return ['nul']; else: return ['R']; elif a == 0 and b != 0: return [-c/b]; else: delta = (b**2-4*a*c); if (delta == 0): return [-b/(2*a)]; elif (delta > 0): delta = delta**0.5; return [(-b-delta)/(2*a), (-b+delta)/(2*a)]; else: return ['nul']; #例6 def sum100(N): result = 0; for i in range(1, N+1): result += i; return result; </span>
<span style="font-size:18px;">#例7 def fun7(): product = 200; year = 2005; while (product <= 300): product *= 1.05; year += 1; return year; #例1 def funSketch(x): return x**3+3*x**2-24*x+30; def sketch(): array = []; for i in range(-5, 6): array.append(funSketch(i)); return array; #例2 def average(literature, math, english): return (literature + math + english)/3; #例3 def assign(x, num): return num; #例4 def swap(A, B): return [B, A]; #例5 def fun5(x): return abs(x);</span>
<span style="font-size:18px;">#例6 def fun6(): inputs = input('输入a, b, c, 逗号隔开: ').split(','); a = float(inputs[0]); b = float(inputs[1]); c = float(inputs[2]); delta = b**2-4*a*c; if (delta >= 0): p = -b/(2*a); q = delta**0.5/(2*a); if (delta == 0): print(p); else: x1 = p+q; x2 = p-q; print(x1, x2); else: print('方程没有实数根');</span>
<span style="font-size:18px;">#例7 def sortNumber(): inputs = input('输入a, b, c, 逗号隔开: ').split(','); a = float(inputs[0]); b = float(inputs[1]); c = float(inputs[2]); array = [a, b, c]; array = sorted(array, reverse=True); return array; #例8 def funSketch(x): return x**3+3*x**2-24*x+30; def sketch(): array = []; for i in range(-5, 6): array.append(funSketch(i)); return array; #例1 def gcd(m, n): m, n = max(m, n), min(m, n); i = 1; while n: print('step', i, ': ', m, n); i += 1; m, n = n, m % n return m #例2 def poly(x, coef): #coef为多项式按次数从高到低排列的系数矩阵 rank = len(coef); result = 0; for i in range(rank): result *= x; result += coef[i]; return result; #例3 def binToDec(s): sum = 0; for i in range(len(s)): sum *= 2; sum += int(s[i]); return sum; #例4 def kToDec(s, k): #s为k进制数的字符串形式,k为进制,如2, 8, 10, 16等 charTable = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; sum = 0; if k > 32: return 'inf'; for i in range(len(s)): sum *= k; for j in range(k): if s[i] == charTable[j]: sum += j; break; return sum; #例5, 6 def dec2k(N, k): s = ''; a = []; #只提供了36进制数以内的唯一表示对照表 if k > 36: return ''; charTable = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; a.append(N%k); N//=k; while N > 0: a.append(N%k); N//=k; size = len(a); for i in range(size): s += charTable[a[size-i-1]]; return s; </span>
<span style="font-size:18px;">>>> 0.8765 #例2 def fun2(): yes = 0; no = 0; N = 10000; for i in range(N): time_1 = 6.5+random.random(); time_2 = 7 + random.random(); if (time_1 < time_2): yes+=1; else: no+=1; return yes/N; >>> 3.1388 #例3 def fun3(): yes = 0; no = 0; N = 10000; for i in range(N): x = random.random()-0.5; y = random.random()-0.5; if (x**2+y**2<=0.5**2): yes+=1; else: no+=1; return yes/N*4; >>> 1.332 #例4 def fun4(): yes = 0; no = 0; N = 10000; for i in range(N): x = 2*(random.random()-0.5); y = random.random(); if (y >= x**2): yes+=1; else: no+=1; return yes/N*2; </span>
标签:
原文地址:http://blog.csdn.net/mwsister/article/details/51187334