[杂项]其他代码合集


2024.10 作业 - 教科书P109~112整理 代码部分

  1. P119-1
1
2
3
4
5
6
7
8
9
10
11
#! /usr/bin/python3
# encoding=utf-8
score=int(input("领先球队分数"))
score-=3
flag=bool(input("领先球队控球?(0.不是 1. 是)")) # 领先球队控球
score=score-0.5+flag
t=int(input("剩余秒数:"))
if score**2>t:
print("安全")
else:
print("不安全")
  1. P119-2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #! /usr/bin/python3
    # encoding=utf-8

    h = 100
    sumh = 0

    for i in range(10):
    sumh+=h
    h/=2
    sumh+=h
    print("共经过:"+str(sumh)+"米","当前高度:"+str(h)+"米")
  2. P119-3

1
2
3
4
5
6
7
8
9
10
11
12
13
#! /usr/bin/python3
# encoding=utf-8

import random

num = random.randint(1,10**10)
print(num,end="")
while num!=1:
if num%2:
num=num*3+1
else:
num/=2
print("=>"+str(num),end="")
  1. P119-4
1
2
3
4
5
6
7
8
9
10
11
12
#! /usr/bin/python3
# encoding=utf-8

file = open("score.txt")
cnt=[0]*5
while s:=file.readline():
score = int(s.split()[1])
level = max(0,min(score,99)//10-5)
cnt[level]+=1

for i in range(5):
print(chr(69-i)+":"+str(cnt[i]))
  1. P119-5
1
2
3
4
5
6
7
8
9
10
#! /usr/bin/python3
# encoding=utf-8

head=35
feet=94
for rab in range(36):
chi = head-rab
if chi*2+rab*4==feet:
print("鸡:"+str(chi),"兔:"+str(rab))
break
  1. P120-121
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /usr/bin/python3
# encoding=utf-8

import pandas as pd

df = pd.read_excel("test.xlsx")

cnt = len(df.groupby("商品名称").indices)

df1 = df.groupby("订单号")

cnt = {}

for i in df1:
product=i[1]["商品名称"]
for j in product:
for k in product:
if j!=k:
try:
cnt[(j,k)]+=1
except:
cnt[(j,k)]=1

maximum=0
name=("","")

for i in cnt:
if cnt[i]>maximum:
maximum=cnt[i]
name=i
print(maximum,name)

  1. P112-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#! /usr/bin/python3
# encoding=utf-8

cost = int(input())

res=[0]*4

res[3]=cost//300*180
cost%=300

if cost<=70:
res[0]+=cost
elif 70<=cost<=100:
res[1]+=100
elif 100<=cost<=130:
res[0]+=cost-100
res[1]+=100
elif 130<=cost<=200:
res[2]+=200
elif 200<=cost<=250:
res[2]+=200
res[0]+=cost-200
else:
res[3]+=300

print("single:"+str(res[0]),"100-30:"+str(res[1]),"200-70:"+str(res[2]),"300-120:"+str(res[3]),sep='\n')
  1. P112-2
  • (1) 数值积分法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     import numpy as np

    def f(x):
    return 4 / (1 + x**2)

    def trapezoidal_rule(a, b, n):
    x = np.linspace(a, b, n+1)
    y = f(x)
    h = (b - a) / n
    integral = (h / 2) * (y[0] + 2 * np.sum(y[1:n]) + y[n])
    return integral

    a = 0
    b = 1
    n = 1000000

    pi_approx = trapezoidal_rule(a, b, n)
    print("使用数值积分法计算的π值: "+pi_approx)
  • (2) 泰勒级数法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     def calculate_pi(n_terms):
    pi_approx = 0
    for k in range(n_terms):
    pi_approx += ((-1) ** k) / (2 * k + 1)
    return 4 * pi_approx

    n_terms = 1000000
    pi_value = calculate_pi(n_terms)
    print(f"使用泰勒级数法计算的π值: {pi_value}")

  • (3) 韦达公式法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     import math

    def vieta_pi(n_terms):
    a = math.sqrt(2)
    pi_approx = 2 / a
    for _ in range(n_terms - 1):
    a = math.sqrt(2 + a)
    pi_approx *= 2 / a
    return 2 * pi_approx

    n_terms = 1000
    pi_value = vieta_pi(n_terms)
    print("使用韦达公式法计算的π值:"+ pi_value)

  • (4) 拉马努金公式法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 from decimal import Decimal, getcontext
from math import factorial, sqrt

# 设置精度
getcontext().prec = 100

def compute_pi(n):
sum = Decimal(0)
for k in range(n):
numerator = factorial(4*k) * (1103 + 26390*k)
denominator = factorial(k)**4 * 396**(4*k)
sum += Decimal(numerator) / Decimal(denominator)
factor = Decimal(2 * sqrt(2)) / Decimal(9801)
pi = 1 / (factor * sum)
return pi

# 计算前10项
pi_value = compute_pi(10)
print(f"计算得到的π值:{pi_value}")
  • (5) 迭代法
1
2
3
4
5
6
7
8
9
10
 def calculate_pi(iterations):
pi = 0
for k in range(iterations):
pi += (1 / 16**k) * (4 / (8*k + 1) - 2 / (8*k + 4) - 1 / (8*k + 5) - 1 / (8*k + 6))
return pi

# 计算前1000项
pi_value = calculate_pi(1000)
print(f"计算得到的π值:{pi_value}")

很显然除了韦达公式法我看不懂一点(逃

  1. P117-3

    不是我总不能真写一个聊天机器人吧


Author: Mozhi Hong
Reprint policy: All articles in this blog are used except for special statements CC BY-NC-SA 4.0 reprint policy. If reproduced, please indicate source Mozhi Hong !
评论
  TOC