219mze 发表于 2024-8-18 05:31:44

Python第123题:计算直角三角形底边斜边【PythonTip题库300题】


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、编程试题:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">编写一个程序,找出已知面积和高的直角三角形的<span style="color: black;">另一</span>两边(底边及斜边)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">定义函数find_missing_sides(),有两个参数:area(面积)和height(高)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在函数内,计算<span style="color: black;">另一</span>两边(低边base 及 斜边hypotenuse),并将结果<span style="color: black;">做为</span>列表返回。<span style="color: black;">运用</span>area来求出底边,使用毕达哥拉斯定理求出斜边。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该函数应返回两个元素的列表。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例输入</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">24</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例输出</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">解释:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>给定的面积24和高6,<span style="color: black;">运用</span>公式面积 = 0.5 * 底边 * 斜边,<span style="color: black;">咱们</span>得到底边8.0和斜边10.0。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输入的面积和高都是整数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">返回的列表应该<span style="color: black;">包括</span>两个元素,且为浮点数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、代码实现:</span></strong></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/c048a2fecd924d8d942e2d3871c00b38~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899759&amp;x-signature=c5xxCOMi%2BRBLf17wOaD9a%2Bs%2FwiM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可编辑代码如下:</p><span style="color: black;">#!/usr/bin/python3.9</span>
    <span style="color: black;"># -*- coding: utf-8 -*-</span>
    <span style="color: black;">#</span>
    <span style="color: black;"># Copyright (C) 2024 , Inc. All Rights Reserved</span>
    <span style="color: black;">#</span>
    <span style="color: black;"># <span style="color: black;">@Time</span> : 2024/2/25 8:36</span>
    <span style="color: black;"># <span style="color: black;">@Author</span> : fangel</span>
    <span style="color: black;"># <span style="color: black;">@FileName</span> : 123. 计算直角三角形底边及斜边.py</span>
    <span style="color: black;"># <span style="color: black;">@Software</span> : PyCharm</span>


    import math


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">find_missing_sides</span><span style="color: black;">(area, height)</span></span>:
    <span style="color: black;"># 先求底边</span>
    base = <span style="color: black;">2</span> * area / height
    <span style="color: black;"># 再求斜边</span>hypotenuse = math.sqrt(pow(base,<span style="color: black;">2</span>) + pow(height, <span style="color: black;">2</span>))
    <span style="color: black;">return</span>


    <span style="color: black;"># 输入面积</span>
    area = int(input())
    <span style="color: black;"># 输入高</span>
    height = int(input())
    <span style="color: black;"># 调用函数</span>print(find_missing_sides(area, height))<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、代码分析:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本例先<span style="color: black;">按照</span>面积和高求出底边,再求斜边,<span style="color: black;">重视</span>用到了math中的开根号函数sqrt()和幂函数pow()</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4、运行结果:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输入:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></p>




张露zhang 发表于 2024-8-24 11:03:36

楼主的文章非常有意义,提升了我的知识水平。

qzmjef 发表于 2024-11-2 02:27:02

你的话语如春风拂面,温暖了我的心房,真的很感谢。
页: [1]
查看完整版本: Python第123题:计算直角三角形底边斜边【PythonTip题库300题】