GDUFE Python实验1

'''
@Task:实验1
@Author:yjy
@Time:2024/09/20
'''

# 1. 用户输入一个三位自然数,计算并输出其百位、十位、个位上的数字,并求和
num = int(input("请您输入一个三位数:"))
hundred_place = num//100
ten_place = num//10 % 10
single_place = num % 10
print(f"您输入的三位数中百位:{hundred_place},十位:{ten_place},个位:{single_place}")

'''
Method2
divmod(a.b) ==> 把除数和余数运算结果结合起来,返回一个商和余数的元组(a//b,a%b).
举个栗子:divmod(7,2) ==> (3,1)
知道了这点 我们就可以接着往下看如何用这个函数来解决这个问题:
'''
num = int(input("请您输入一个三位数:"))
hundred_place,ten_place = divmod(num,100) # (num//100,num%100)
ten_place,single_place = divmod(ten_place,10) # (ten_place//10,ten_place%10)
print(f"您输入的三位数中百位:{hundred_place},十位:{ten_place},个位:{single_place}")

'''
Method3
map(function, iterable, ...)

在IDLE写的代码:
>>> def square(x) :         # 计算平方数
...     return x ** 2
...
>>> map(square, [1,2,3,4,5])    # 计算列表各个元素的平方
<map object at 0x100d3d550>     # 返回迭代器
>>> list(map(square, [1,2,3,4,5]))   # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))   # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

>>>
'''
num = input('请您输入一个三位数:') # 注意我这里没有墙砖
hundred_place,ten_place,single_place = map(int,num)
print(f"您输入的三位数中百位:{hundred_place},十位:{ten_place},个位:{single_place}")


# 2. 已知三角形的两边长及其夹角,求第三边
# '''
# 采用高中所学的余弦定理
# '''
import math


def find_third_side(a: float, b: float, theta_degrees: float) -> float:
    """
    计算三角形第三边的长度。

    参数:
    a (float): 三角形的第一边
    b (float): 三角形的第二边
    theta_degrees (float): 两边之间的夹角,单位为度

    返回:
    float: 第三边的长度
    """
    # 将角度转换为弧度,因为math.cos使用的是弧度制
    theta_radians = math.radians(theta_degrees)

    # 应用余弦定理计算第三边的长度
    c_squared = a ** 2 + b ** 2 - 2 * a * b * math.cos(theta_radians)

    # 返回第三边的长度,取正平方根 边肯定为正的
    return math.sqrt(c_squared)


if __name__ == '__main__':
    a = 5
    b = 7
    theta_degrees = 60

    third_side = find_third_side(a, b, theta_degrees)
    print(f"第三边的长度为: {third_side:.2f}")

# 3. 任意输入3个英文单词,按字典顺序排序输出
def sort_words():
    """
    任意输入3个英文单词,并按字典顺序排序后输出。
    """
    # 输入3个英文单词
    word1 = input("请输入第一个英文单词: ")
    word2 = input("请输入第二个英文单词: ")
    word3 = input("请输入第三个英文单词: ")

    # 将输入的单词存储到列表中
    words = [word1, word2, word3]

    # 按字典顺序对单词进行排序
    words.sort()

    # 输出排序后的单词
    print("按字典顺序排序后的单词为:")
    for word in words:
        print(word)


# 示例用法
sort_words()

# 4. 任意输入一串数字,按降序排序
def sort_numbers_desc():
    """
    任意输入一串数字,并按降序排序后输出。
    """
    # 输入一串数字,并将它们分割为列表
    numbers = input("请输入一串数字(用空格分隔): ").split()

    # 将字符串列表转换为整数列表
    numbers = [int(num) for num in numbers]

    # 对数字列表按降序排序
    numbers.sort(reverse=True)

    # 输出排序后的数字
    print("按降序排序后的数字为:")
    print(numbers)


# 示例用法
sort_numbers_desc()

# 5. 输入一个温度值,进行转换,要求能进行摄氏温度和华氏温度之间的互转。
def celsius_to_fahrenheit(celsius: float) -> float:
    """
    将摄氏温度转换为华氏温度。

    参数:
    celsius (float): 摄氏温度

    返回:
    float: 转换后的华氏温度
    """
    return (celsius * 9 / 5) + 32


def fahrenheit_to_celsius(fahrenheit: float) -> float:
    """
    将华氏温度转换为摄氏温度。

    参数:
    fahrenheit (float): 华氏温度

    返回:
    float: 转换后的摄氏温度
    """
    return (fahrenheit - 32) * 5 / 9


def temperature_conversion():
    """
    根据用户输入的温度值和温标,进行摄氏温度和华氏温度的转换。
    """
    # 用户选择转换方向
    print("请选择转换类型:")
    print("1. 摄氏温度转换为华氏温度")
    print("2. 华氏温度转换为摄氏温度")
    choice = input("请输入选项 (1 或 2): ")

    # 根据选择进行相应的温度转换
    if choice == '1':
        celsius = float(input("请输入摄氏温度: "))
        fahrenheit = celsius_to_fahrenheit(celsius)
        print(f"{celsius:.2f} 摄氏度 = {fahrenheit:.2f} 华氏度")
    elif choice == '2':
        fahrenheit = float(input("请输入华氏温度: "))
        celsius = fahrenheit_to_celsius(fahrenheit)
        print(f"{fahrenheit:.2f} 华氏度 = {celsius:.2f} 摄氏度")
    else:
        print("无效的选项,请输入1或2。")


# 示例用法
temperature_conversion()

# 6. 编写程序:输入两个集合A 和B(使用input函数通过键盘赋值),输出它们的交集、并集和差集
def get_set_input(prompt: str) -> set:
    """
    通过键盘输入集合,并将输入的内容转换为集合类型。

    参数:
    prompt (str): 输入提示信息

    返回:
    set: 输入的集合
    """
    # 输入集合元素,以空格分隔,使用split()将字符串分割为列表,再转换为集合
    return set(input(prompt).split())


def set_operations():
    """
    输入两个集合A和B,输出它们的交集、并集和差集。
    """
    # 输入集合A和集合B
    set_A = get_set_input("请输入集合A的元素(用空格分隔): ")
    set_B = get_set_input("请输入集合B的元素(用空格分隔): ")

    # 计算交集、并集和差集
    intersection = set_A & set_B  # 交集
    union = set_A | set_B  # 并集
    difference = set_A - set_B  # 差集(A - B)

    # 输出结果
    print(f"集合A: {set_A}")
    print(f"集合B: {set_B}")
    print(f"A 和 B 的交集: {intersection}")
    print(f"A 和 B 的并集: {union}")
    print(f"A 和 B 的差集(A - B): {difference}")


# 示例用法
set_operations()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值