zoukankan      html  css  js  c++  java
  • 2020春季学期第八周学习总结

          本周学习了python编程的一些常见算法编程实现,总结如下:

    1、蒙特·卡罗法计算圆周率

     1 import random
     2 print("掷飞镖次数:")
     3 number = int(input())
     4 k = 0
     5 for i in range(1, number + 1):
     6     x = random.uniform(-1, 1)  # x随机取到-1到1的随机数
     7     y = random.uniform(-1, 1)  # Y随机取到-1到1的随机数
     8     if (x * x + y * y <= 1):
     9         k += 1
    10 print("输出值为:")
    11 print(k / number * 4)

    运行结果:

    2、模拟页面调度LRU算法

     1 def LRU(Pages, Num, n):
     2     temp = []
     3     t= 0
     4     for p in lst:
     5         num = len(temp)
     6         if num < n:
     7             t += 1
     8             temp.append(p)
     9         elif num == n:  # 要访问的新页面已在主存块中
    10             if p in temp:  # 处理“主存块”,把最新访问的页面交换到列表尾部
    11                 pos = temp.index(p)
    12                 temp = temp[:pos] + temp[pos + 1:] + [p]
    13             else:  # 把最早访问的页面踢掉,调入新页面
    14                 temp.pop(0)
    15                 temp.append(p)
    16                 t+= 1
    17     return t
    18 n = int(input())
    19 lst = tuple(input().split(" "))
    20 print(LRU(lst, 3, n))

    运行结果:

    3、杨辉三角形

     1 def yang(i, j):  # i为行,j为列
     2     if j == 0 or j == i:
     3         return 1
     4     else:
     5         return yang(i - 1, j) + yang(i - 1, j - 1)
     6 
     7 a=int(input())
     8 for i in range(0, a):
     9     print()
    10     for n in range(0, a - i):
    11         print(" ", end="")  # 控制每一行前面的空格
    12     for j in range(0, i + 1):
    13         print(yang(i, j), "", end="")

    运行结果:

    4、查找鞍点

     1 list1=[]
     2 list_max=[]
     3 list_min=[]
     4 for i in range(5):
     5     list=input().split()
     6     list_int=[int(x) for x in list]
     7     list1.append(list_int )
     8     k=0
     9     for j in list_int: 
    10         if j==max(list_int):
    11             break
    12         k=k+1
    13     list_max.append([i+1,k+1, max(list_int)])
    14 #print(list_max)
    15 list2=[[list1[j][i] for j in range(5)] for i  in range(5)]
    16 i=0
    17 for l in list2:
    18     i=i+1
    19     k = 0
    20     for j in l  :
    21         if j == min(l):
    22             break
    23         k = k + 1
    24     list_min.append([ k + 1,i, min(l)])
    25 #print(list_min)
    26 
    27 for i in list_max:
    28     for j in list_min:
    29         if str(i)==str(j):
    30             print(i,end=" ")

    运行结果:

    5、牛顿迭代法

     1 def iteration(a, b, c, d, X):
     2     x = X
     3     if a == 0 and c ** 2 - 4 * b * d < 0:
     4         print("无解")
     5     elif a == 0 and b == 0 and c == 0 and d != 0:
     6         print("无解")
     7     elif a == 0 and b == 0 and c == 0 and d == 0:
     8         print("恒等")
     9     else:
    10         while abs(a * x * x * x + b * x * x + c * x + d) > 0.000001:
    11             x = x - (a * x * x * x + b * x * x + c * x + d) / (3 * a * x * x + 2 * b * x + c)
    12         print("x=%.2f" % x,end=" ")
    13 a, b, c, d, x = input().split()
    14 iteration(float(a), float(b), float(c), float(d), float(x))

    运行结果:

  • 相关阅读:
    【ABAP系列】SAP LSMW(摘自官网)
    【ABAP系列】SAP ABAP POPUP弹出框自建内容
    【ABAP系列】SAP ABAP ALV中的TOP_OF_PAGE添加任意图标
    彻底关闭Windows Defender丨Win10
    word中怎样设置页码包含总页数
    10款流行的Markdown编辑器,总有一款适合你
    MyEclipse安装插件
    Eclipse集成SonarLint
    MyEclipse中阿里JAVA代码规范插件(P3C)的安装及使用
    详述 IntelliJ IDEA 插件的安装及使用方法
  • 原文地址:https://www.cnblogs.com/lover995/p/12681677.html
Copyright © 2011-2022 走看看