Python函数(二)※

Python函数(二)※

第1关:函数的参数 - 搭建函数房子的砖

题目

在这里插入图片描述

代码

#coding=utf-8
 
#创建一个空列表numbers
numbers = []
 
#str用来存储输入的数字字符串,lst1是将输入的字符串用空格分割,存储为列表
str = input()
lst1 = str.split(' ')
 
#将输入的数字字符串转换为整型并赋值给numbers列表
for i in range(len(lst1)):
   numbers.append(int(lst1.pop()))
 
# 请在此添加函数plus的代码,函数参数为一个列表,对列表中的数值元素进行累加求和
#********** Begin *********#
def plus(*numbers):
   add=0
   for i in numbers:
      add += i
   return add
#********** End **********#
 
#调用plus函数,并将返回结果存储到变量d中
d = plus(*numbers)
print(d)

第2关:函数的返回值 - 可有可无的return

题目

在这里插入图片描述

代码


#coding=utf-8
 
#输入两个正整数a,b
a = int(input())
b = int(input())
 
# 请在此添加函数gcd代码,求两个正整数的最大公约数
#********** Begin *********#
def gcd(a,b):
    if(a<b):
        t=a
        a=b
        b=t
    while(a!=0):
        temp=b%a
        b=a
        a=temp
    return b
#********** End **********#
 
#调用函数,并输出最大公约数
print(gcd(a,b))

第3关:函数的使用范围:Python作用域

题目

在这里插入图片描述

代码

#coding=utf-8
 
#输入两个正整数a,b
a = int(input())
b = int(input())
 
# 请在此添加一个private函数_gcd()求两个正整数的最大公约数
#********** Begin *********#
def gcd(a,b):
    if(a<b):
        t=a
        a=b
        b=t
    while(a!=0):
        temp=b%a
        b=a
        a=temp
    return b
#********** End **********#
 
#请在此添加一个public函数lcm(),在lcm()函数中调用_gcd()函数,求两个正整数的最小公倍数
#********** Begin *********#
def lcm(a,b):
    c=gcd(a,b)
    s=int(a*b/c)
    return s
#********** End **********# 
#调用函数,并输出a,b的最小公倍数
print(lcm(a,b))

第4关: 函数综合训练※

题目

在这里插入图片描述

代码

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt     #导入matplotlib.pyplot
 
# 第一题
######## begin ###########
# 请编写函数triArea(a ,b , c),返回三角形的面积
def triArea(a,b,c):
    p=(a+b+c)/2
    S=pow(p*(p-a)*(p-b)*(p-c),1/2)
    return S
######## end #########
 
S1 = triArea(9.8, 9.3, 6.4)
S2 = triArea(2.9, 4.1, 4.7)
S3 = triArea(2.0, 1.4, 2.3)
print('%.6f' %(S1-S2+S3))
 
# 第二题
######## begin ###########
# 请编写函数isPrime(x)。判断x是否是素数,返回True或者False
def isPrime(x):
    isPrime=1
    if x==1:
        isPrime=0
    for i in range(2,x//2):
        if x%i==0:
            isPrime=0
            break
    if isPrime:
        return True
    else:
        return False      
#######  end    ############
 
 
def Goldbach(N):  # 将N分解成两素数之和
    if N < 6 or N % 2 == 1:  # 若N小于6或N为奇数
        print('N应该是大于等于6的偶数')
    else:
        # 循环判断,得到符合要求的小于N的两个素数,并打印
        for x in range(2, N // 2):  # 想想为什么是从2到N/2
            # 调用isPrime函数,得到符合要求的小于N的两个素数
            ######## begin ###########
            if isPrime(x)==True and isPrime(N-x)==True:
            ######## end ###########
             print(N, '=', x, '+', N - x)
             break
 
 
N = int(input())
Goldbach(N)
 
# 第三题
# calBombTrace 函数
def calBombTrace(theta):
    v0, g, n = 300, 9.8, 30
    theta_d = np.radians(theta)     #因numpy中cos、sin的输入为弧度值,故先将theta从度转换为弧度
    v0_x = v0*np.cos(theta_d)       #炮弹水平初速度
    v0_y = v0*np.sin(theta_d)       #炮弹垂直初速度
    tmax = 2*v0_y/g                 #落地时间,即v0_y*t-0.5gtt=0的解
    t = np.linspace(0, tmax, n)     #n个时刻
    xt = v0_x*t                     #n个时刻的横坐标
    yt = v0_y*t-1/2*g*t**2          #n个时刻的纵坐标
    return xt, yt
 
for theta in [30, 45, 60, 75]:
    ############ begin #########
    # 调用函数,得到theta返回的值,并存储到xt与yt变量
    xt,yt=calBombTrace(theta)
    ########### end #############
    plt.plot(xt, yt)
 
plt.grid('on')
plt.axis([0, 10000, 0, 5000])
plt.savefig('./src/step4/res/轨迹.png')
 
plt.close()
print([round(x,6) for x in xt])
print([round(x,6) for x in yt])
 
# 第四题
# 在此添加代码,编写函数f(x),计算f(x)的值
########### begin #############
def f(x):
    a=((np.e)**x)*(np.sin(x))
    return a
########### end #############
 
# 在此添加代码,编写函数pt(a, b),利用勾股定理计算直角三角形的斜边长
########### begin #############
def pt(a,b):
    c=pow((a*a+b*b),1/2)
    return c
########### end #############
 
n = 1000  # 细分成n个子区间
x = np.linspace(0, np.pi, n + 1)  # n个子区间的n+1个端点
l, h = 0, np.pi / n  # l为曲线长度、h为子区间宽度
for i in range(n):  # 对每个子区间
    li = pt(f(x[i + 1]) - f(x[i]), h)  # 曲线在第i个子区间的近似长度
    l = l + li  # 将航渡累加到l中
 
print('l = %.6f' %l)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值