Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235734
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-10-26 17:23:02

1.    实现1-100的所有的和

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-10-26 15:41
  5. # @file :sum.py

  6. # 方法1 for循环实现
  7.  sum1 = 0
  8.  for i in xrange(1,101):
  9.  sum1 += i

  10.  print "1+2+3+...+100= %d" % sum1

  11. # 方法2 while循环实现

  12. sum2 = 0
  13. n = 0
  14. while True:
  15.     sum2 += n
  16.     n += 1
  17.     if n>100: break

  18. print "1+2+3+...+100= %d" % sum2
执行结果:
1+2+3+...+100= 5050
1+2+3+...+100= 5050

2.    实现1-500所有奇数的和

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-10-26 15:57
  5. # @file :qishu.py

  6. sum1 = 0
  7. for i in xrange(1,501):
  8.     if i % 2 != 0:
  9.         sum1 += i

  10. print "1-500所有奇数的和:%d " % sum1

  11. sum2 = 0
  12. for i in xrange(1,501,2):
  13.         sum2 += i

  14. print "1-500所有奇数的和:%d " % sum2
执行结果:
1-500所有奇数的和:62500 
1-500所有奇数的和:62500 
3.    求1+ 2! + 3! + 4! + ……20!的和

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-10-26 13:35
  5. # @file :jiechen.py

  6. # 方法1
  7. i = 1
  8. sum1 = 0
  9. sum2 = 1
  10. for i in xrange(1,21):
  11.     sum2 = sum2 * i
  12.     sum1 = sum1 + sum2
  13.     print "1-%d的阶乘和:%s" % (i, sum1)

  14. # 方法2
  15. import math
  16. sum1 = 0
  17. for i in xrange(1,21):
  18.     sum1 += math.factorial(i)
  19.     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]

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-10-26 16:06
  5. # @file :paixu.py

  6. # 方法1
  7. alist = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
  8. alist.sort()
  9. print "sort方法实现"
  10. print alist
  11. print('*' * 51)
  12. # 插入排序
  13. lists = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
  14. def insert_sort(lists):
  15.     count = len(lists)
  16.     for i in range(1, count):
  17.         key = lists[i]
  18.         j = i - 1
  19.         while j >= 0:
  20.             if lists[j] > key:
  21.                 lists[j + 1] = lists[j]
  22.                 lists[j] = key
  23.             j -= 1
  24.     print "插入排序"
  25.     print lists

  26. # 希尔排序
  27. def shell_sort(lists):
  28.     count = len(lists)
  29.     step = 2
  30.     group = count / step
  31.     while group > 0:
  32.         for i in range(0, group):
  33.             j = i + group
  34.             while j < count:
  35.                 k = j - group
  36.                 key = lists[j]
  37.                 while k >= 0:
  38.                     if lists[k] > key:
  39.                         lists[k + group] = lists[k]
  40.                         lists[k] = key
  41.                     k -= group
  42.                 j += group
  43.         group /= step
  44.     print "希尔排序"
  45.     print lists

  46. #冒泡排序
  47. def bubble_sort(lists):
  48.     count = len(lists)
  49.     for i in range(0, count):
  50.         for j in range(i + 1, count):
  51.             if lists[i] > lists[j]:
  52.                 lists[i], lists[j] = lists[j], lists[i]
  53.     print "冒泡排序"
  54.     print lists

  55. # 选择排序
  56. def select_sort(lists):
  57.     count = len(lists)
  58.     for i in range(0, count):
  59.         min = i
  60.         for j in range(i + 1, count):
  61.             if lists[min] > lists[j]:
  62.                 min = j
  63.         lists[min], lists[i] = lists[i], lists[min]
  64.     print "选择排序"
  65.     print lists

  66. # 堆排序
  67. def adjust_heap(lists, i, size):
  68.     lchild = 2 * i + 1
  69.     rchild = 2 * i + 2
  70.     max = i
  71.     if i < size / 2:
  72.         if lchild < size and lists[lchild] > lists[max]:
  73.             max = lchild
  74.         if rchild < size and lists[rchild] > lists[max]:
  75.             max = rchild
  76.         if max != i:
  77.             lists[max], lists[i] = lists[i], lists[max]
  78.             adjust_heap(lists, max, size)

  79. def build_heap(lists, size):
  80.     for i in range(0, (size/2))[::-1]:
  81.         adjust_heap(lists, i, size)

  82. def heap_sort(lists):
  83.     size = len(lists)
  84.     build_heap(lists, size)
  85.     for i in range(0, size)[::-1]:
  86.         lists[0], lists[i] = lists[i], lists[0]
  87.         adjust_heap(lists, 0, i)
  88.     print "堆排序"
  89.     print lists

  90. if __name__ == '__main__':
  91.     insert_sort(lists)
  92.     shell_sort(lists)
  93.     bubble_sort(lists)
  94.     select_sort(lists)
  95.     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) |
给主人留下些什么吧!~~