1. 实现1-100的所有的和
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-10-26 15:41
-
# @file :sum.py
-
-
# 方法1 for循环实现
-
sum1 = 0
-
for i in xrange(1,101):
-
sum1 += i
-
-
print "1+2+3+...+100= %d" % sum1
-
-
# 方法2 while循环实现
-
-
sum2 = 0
-
n = 0
-
while True:
-
sum2 += n
-
n += 1
-
if n>100: break
-
-
print "1+2+3+...+100= %d" % sum2
执行结果:
1+2+3+...+100= 5050
1+2+3+...+100= 5050
2. 实现1-500所有奇数的和
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-10-26 15:57
-
# @file :qishu.py
-
-
sum1 = 0
-
for i in xrange(1,501):
-
if i % 2 != 0:
-
sum1 += i
-
-
print "1-500所有奇数的和:%d " % sum1
-
-
sum2 = 0
-
for i in xrange(1,501,2):
-
sum2 += i
-
-
print "1-500所有奇数的和:%d " % sum2
执行结果:
1-500所有奇数的和:62500
1-500所有奇数的和:62500
3. 求1+ 2! + 3! + 4! + ……20!的和
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-10-26 13:35
-
# @file :jiechen.py
-
-
# 方法1
-
i = 1
-
sum1 = 0
-
sum2 = 1
-
for i in xrange(1,21):
-
sum2 = sum2 * i
-
sum1 = sum1 + sum2
-
print "1-%d的阶乘和:%s" % (i, sum1)
-
-
# 方法2
-
import math
-
sum1 = 0
-
for i in xrange(1,21):
-
sum1 += math.factorial(i)
-
print "1-%d的阶乘和:%s" % (i, sum1)
执行结果:
1-1的阶乘和:1
1-2的阶乘和:3
1-3的阶乘和:9
1-4的阶乘和:33
1-5的阶乘和:153
1-6的阶乘和:873
1-7的阶乘和:5913
1-8的阶乘和:46233
1-9的阶乘和:409113
1-10的阶乘和:4037913
1-11的阶乘和:43954713
1-12的阶乘和:522956313
1-13的阶乘和:6749977113
1-14的阶乘和:93928268313
1-15的阶乘和:1401602636313
1-16的阶乘和:22324392524313
1-17的阶乘和:378011820620313
1-18的阶乘和:6780385526348313
1-19的阶乘和:128425485935180313
1-20的阶乘和:2561327494111820313
4. 对指定一个list进行排序[2,32,43,453,54,6,576,5,7,6,8,78,7,89]
-
#!/usr/bin/env python
-
# -*- coding:utf-8 -*-
-
# Author :Alvin.xie
-
# @Time :2017-10-26 16:06
-
# @file :paixu.py
-
-
# 方法1
-
alist = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
-
alist.sort()
-
print "sort方法实现"
-
print alist
-
print('*' * 51)
-
# 插入排序
-
lists = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
-
def insert_sort(lists):
-
count = len(lists)
-
for i in range(1, count):
-
key = lists[i]
-
j = i - 1
-
while j >= 0:
-
if lists[j] > key:
-
lists[j + 1] = lists[j]
-
lists[j] = key
-
j -= 1
-
print "插入排序"
-
print lists
-
-
# 希尔排序
-
def shell_sort(lists):
-
count = len(lists)
-
step = 2
-
group = count / step
-
while group > 0:
-
for i in range(0, group):
-
j = i + group
-
while j < count:
-
k = j - group
-
key = lists[j]
-
while k >= 0:
-
if lists[k] > key:
-
lists[k + group] = lists[k]
-
lists[k] = key
-
k -= group
-
j += group
-
group /= step
-
print "希尔排序"
-
print lists
-
-
#冒泡排序
-
def bubble_sort(lists):
-
count = len(lists)
-
for i in range(0, count):
-
for j in range(i + 1, count):
-
if lists[i] > lists[j]:
-
lists[i], lists[j] = lists[j], lists[i]
-
print "冒泡排序"
-
print lists
-
-
# 选择排序
-
def select_sort(lists):
-
count = len(lists)
-
for i in range(0, count):
-
min = i
-
for j in range(i + 1, count):
-
if lists[min] > lists[j]:
-
min = j
-
lists[min], lists[i] = lists[i], lists[min]
-
print "选择排序"
-
print lists
-
-
# 堆排序
-
def adjust_heap(lists, i, size):
-
lchild = 2 * i + 1
-
rchild = 2 * i + 2
-
max = i
-
if i < size / 2:
-
if lchild < size and lists[lchild] > lists[max]:
-
max = lchild
-
if rchild < size and lists[rchild] > lists[max]:
-
max = rchild
-
if max != i:
-
lists[max], lists[i] = lists[i], lists[max]
-
adjust_heap(lists, max, size)
-
-
def build_heap(lists, size):
-
for i in range(0, (size/2))[::-1]:
-
adjust_heap(lists, i, size)
-
-
def heap_sort(lists):
-
size = len(lists)
-
build_heap(lists, size)
-
for i in range(0, size)[::-1]:
-
lists[0], lists[i] = lists[i], lists[0]
-
adjust_heap(lists, 0, i)
-
print "堆排序"
-
print lists
-
-
if __name__ == '__main__':
-
insert_sort(lists)
-
shell_sort(lists)
-
bubble_sort(lists)
-
select_sort(lists)
-
heap_sort(lists)
执行结果:
sort方法实现
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
***************************************
插入排序
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
希尔排序
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
冒泡排序
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
选择排序
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
堆排序
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
阅读(1274) | 评论(0) | 转发(0) |