上一页 1 ··· 39 40 41 42 43 44 45 46 47 ··· 56 下一页
摘要: 基本排序算法,包括冒泡排序,插入排序,选择排序,堆排序,快速排序等。【冒泡排序】复杂度是n*n#coding:utf8#author:HaxtraZ#description:冒泡排序def bubblesort1(a): #每次找到一个最小元素,放到数组首部 n=len(a) for i in range(0,n-1): swapped=False for j in range(n-1,i,-1): if a[j]a[j+1]: a[j],a[j+1]=a[j+1],a[j] ... 阅读全文
posted @ 2013-08-22 15:51 ChrisZZ 阅读(1166) 评论(1) 推荐(0) 编辑
摘要: MIT6.006是算法导论课,Lec03主要讲插入排序,归并排序,以及分析方法(递归树)等。插入排序,可以分为线性插入排序、二分插入排序,区别在于当把数组中某元素插入到前面的有序列表中时,前者遍历,后者二分,后者更加稳定。归并排序,是用分治思想处理,先分别排序,再合并。递归树,我的理解是算法消耗时间T(n)用树状的结构,表示每次递归消耗的时间,这些时间累加就是T(n),而递归树的每一行和相邻行之间的关系也是比较容易观察的,这就容易写出时间复杂度的表达式了。另外有主定理可以使用。参考了《算法导论》和网络上的资源,以下是我修改后的代码:#coding:utf8#插入排序 版本1(线性插入排序)de 阅读全文
posted @ 2013-08-20 19:45 ChrisZZ 阅读(727) 评论(0) 推荐(0) 编辑
摘要: MIT6.006是算法导论,Lec02讲的是Document Distance(文档距离),比如比较两个文档相似度或者搜索引擎中都会用到。计算步骤为:1.将每个文档分离为单词2.统计词频3.计算点积(并做除法)说明:1.“单词”指的是字母和数字(alphanumeric)2.每个文档统计完词频后得到的list,可看作一个向量3.两个文档间的相似度,是相似的单词除以总的单词,类似于两个向量的夹角公式MIT6.006下载的相关资源中,给出了8个逐渐改善的代码版本,但本质都是一样的。代码8短小精悍,我添加了一些中文注释#coding:utf8#description:计算文档距离import sys 阅读全文
posted @ 2013-08-20 13:34 ChrisZZ 阅读(1449) 评论(0) 推荐(0) 编辑
摘要: MIT6.006是Algo Intro这门课,据说语言使用pythonLec01是讲peak finding,也就是峰值点具体为:一维情况下一个数组中a[i]>a[i-1]且a[i]>a[i+1]那么它是peak 边界时检查一个方向就ok二维情况下需要某元素x比四个相邻元素都大,边界也类似一维去处理只要找到一个peak返回就好复杂度:一维用二分,log n二维先二分,二分后的一维数组遍历一下,所以是O(n*log n)代码:# coding:utf8# MIT6.006 Lec01# peakfinder in 1D condition# --by HaxtraZdef peakf 阅读全文
posted @ 2013-08-19 20:35 ChrisZZ 阅读(869) 评论(0) 推荐(0) 编辑
摘要: A. Theatre Squaretime limit per test2 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard outputTheatre Square in the capital city of Berland has a rectangular shape with the sizen × mmeters. On the occasion of the city's anniversary, a decision was taken to pave the S 阅读全文
posted @ 2013-08-18 18:57 ChrisZZ 阅读(518) 评论(0) 推荐(0) 编辑
摘要: 网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的主程序:#coding:utf8#! /usr/bin/env python# 注释说明:shape表示一个俄罗斯方块形状 cell表示一个小方块import sysfrom random import choiceimport pygamefrom pygame.locals import *from block import O, I, S, Z, L, J, TCOLS = 16ROWS = 20CELLS = COLS * ROWSCELLPX = 32 # 每个cell. 阅读全文
posted @ 2013-08-18 16:45 ChrisZZ 阅读(575) 评论(0) 推荐(0) 编辑
摘要: pygame是用来写2D游戏的。实现斜线运动,无非是在x和y两个方向上分运动的合成。x方向上的运动,当撞到边界的时候取相反速度就好了。这里是用网球王子中的图片,以及一个网球实现,效果截图:注意看,那个方块形状的网球是会动的,撞到边界就反弹,时刻匀速(这情况太理想了。。。)代码:# coding:utf-8bgimg = 'teni.jpg'spimg = 'ball.jpg'import pygamefrom pygame.locals import *from sys import exitpygame.init()screen = pygame.displa 阅读全文
posted @ 2013-08-17 13:45 ChrisZZ 阅读(886) 评论(0) 推荐(0) 编辑
摘要: 用hash解决。我python代码消耗很多内存,好在代码比C++短很多n = int(raw_input())mylist = raw_input().split(' ')i = 0zid = {}for x in mylist: zid[x] = i i += 1q = int(raw_input())m = raw_input().split(' ')ans1 = 0ans2 = 0for y in m: tmp = zid[y] ans1 += tmp + 1 ans2 += n - tmpprint ans1, ans2 阅读全文
posted @ 2013-08-17 01:39 ChrisZZ 阅读(252) 评论(0) 推荐(0) 编辑
摘要: codeforces题目,用python写本题输入三个点坐标,考察叉积,若大于0则right,小于0则left,等于0则towards代码:a = raw_input().split()b = raw_input().split()c = raw_input().split()xa = int(a[0])ya = int(a[1])xb = int(b[0])yb = int(b[1])xc = int(c[0])yc = int(c[1])x1 = xb - xax2 = xb - xcy1 = yb - yay2 = yb - ycres = x1 * y2 - x2 * y1if res 阅读全文
posted @ 2013-08-17 00:35 ChrisZZ 阅读(369) 评论(0) 推荐(0) 编辑
摘要: Python简明教程,此资源位于http://woodpecker.org.cn/abyteofpython_cn/chinese/ s=u'中文字符' #u表示unicode,使用u之后能正常显示中文 s='''多行文本 这是第二哈''' #'''表示多行注释。也可以用""" 布尔型:True,False docString:文档字符串。eg:# Filename : nice.py# encoding:utf8def printMax(x, y): u''& 阅读全文
posted @ 2013-08-15 18:04 ChrisZZ 阅读(419) 评论(0) 推荐(0) 编辑
上一页 1 ··· 39 40 41 42 43 44 45 46 47 ··· 56 下一页