代码: #!/usr/bin/env python # encoding: utf-8 """ @author: 侠之大者kamil @file: 2.回形矩阵.py @time: 2016/3/21 10:51 """ def sankeMatric(n): m = [[0] * n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): m[p][…
def bsm(n): a = [[0]*n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): a[p][i] = t t += 1 for i in range(p,q): a[i][q] = t t += 1 for i in range(q,p,-1): a[q][i] = t t += 1 for i in range(q,p,-1): a[i][p] = t t += 1 p += 1 q…
python算法(一) 一.求数x的因子 x=100 divisors=()#初始化空的元组 for i in range(1,x): if x%i==0: divisors=divisors+(i,) print divisors 二.求数x各个数位之和 sumdigits=0 for c in str(1952): sumdigits +=int(c) print sumdigits print sumdigits 三.鸡兔同笼以及变形 1.有鸡兔两种,共有x个头,y只脚,求解鸡兔各有几只?…
Python算法:推导.递归和规约 注:本节中我给定下面三个重要词汇的中文翻译分别是:Induction(推导).Recursion(递归)和Reduction(规约) 本节主要介绍算法设计的三个核心知识:Induction(推导).Recursion(递归)和Reduction(规约),这是原书的重点和难点部分 正如标题所示,本节主要介绍下面三部分内容: • Reduction means transforming one problem to another. We normally red…