Python
MagHSK
DYYZ-MagHSK
展开
-
Python - 求斐波那契数列前N项之和
n = int(input("Input N: "))a = 0b = 1sum = 0for i in range(n):sum += aa, b = b, a + bprint("The sum of", n, "FIB is", sum,"!")原创 2014-11-11 21:27:40 · 21114 阅读 · 0 评论 -
Python - 素数筛
def shai(n): prim = list() check = list([1] * (n + 1)) for i in range(2, n + 1): if(check[i] == 1): prim.append(i) print(i) for j in prim: if(j * i > n): break check[j * i] = 0原创 2014-11-11 21:12:36 · 557 阅读 · 0 评论 -
Python - KMP算法
def KMP_match(src, pat, nex): i = 0 j = 0 n = len(src) m = len(pat) while(i < n): if(src[i] == pat[j]): i += 1 j += 1 if(j >= m): return i - m else: if(j == 0): i += 1原创 2014-11-13 12:54:02 · 764 阅读 · 1 评论