7-1 烟囱的高度 分数 8 全屏浏览题目 切换布局 作者 李祥 单位 湖北经济学院 已知人离烟囱的距离为 a,人的眼睛离地高度为 b,人看烟囱顶时的仰角为 θ,求烟囱的高度 h。 输入格式 a b θ 注:a、b 的单位为米,均为小数。仰角 θ 的单位为角度,包括度、分和秒数,其中度、分为整数,秒数为小数。 输出格式 h 注:h 的单位为米。 要求:输出2位小数,末位四舍五入。
时间: 2023-06-26 22:05:25 浏览: 78
题目描述
已知人离烟囱的距离为 $a$,人的眼睛离地高度为 $b$,人看烟囱顶时的仰角为 $\theta$,求烟囱的高度 $h$。
输入格式
$a\ b\ \theta$
输出格式
$h$
数据范围
$a,b,h,\theta \in [0,100]$
输入样例
10.0 1.0 30
输出样例
14.64
算法1
(三角函数) $O(1)$
根据三角函数中的正切函数可知:
$$\tan \theta = \frac{h}{a}$$
因此,
$$h = a \tan \theta$$
题目中给出的是仰角,而正切函数需要的是俯角,因此需要将仰角转换为俯角。俯角为 $90^\circ - \theta$,因此有:
$$\tan (90^\circ - \theta) = \cot \theta = \frac{b}{a}$$
因此,代入原式得:
$$h = a \tan \theta = a \frac{\tan \theta}{\cot \theta} = \frac{a}{\frac{b}{a}} = \frac{a^2}{b}$$
C++ 代码
时间复杂度
$O(1)$
算法2
(勾股定理) $O(1)$
根据勾股定理可知:
$$h = \sqrt{(a^2 + b^2) - 2ab \cos \theta}$$
因此,代入原式得:
$$h = \sqrt{a^2 + b^2 - 2ab \cos \theta}$$
C++ 代码
时间复杂度
$O(1)$
相关问题
6-1 判断日期 分数 20 作者 李祥 单位 湖北经济学院 请编写函数,判断日期是否有效。 函数原型 int IsValidDate(int year, int month, int day);
好的,根据您的要求,我为您编写了如下的函数,用于判断给定的日期是否合法:
```
int IsValidDate(int year, int month, int day) {
if (year < 1 || month < 1 || month > 12 || day < 1) {
return 0;
}
int max_day = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
max_day = 30;
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
max_day = 29;
} else {
max_day = 28;
}
}
return day <= max_day;
}
```
这个函数接受三个参数:年份、月份和日期,返回一个整数值,如果日期是合法的,则返回1,否则返回0。
该函数的实现思路如下:
首先,如果年份、月份或日期中有任何一个小于1,则该日期无效,直接返回0。
其次,根据每个月的天数以及闰年的情况,计算该月最大的天数。对于4、6、9、11月,最大天数为30;对于2月,如果是闰年,则最大天数为29,否则为28;其余月份最大天数为31。
最后,比较给定的日期是否小于等于该月最大天数,如果是,则该日期有效,返回1,否则返回0。
希望这个函数能够满足您的需求!
6-1 求两点间的距离(填空) 分数 5 全屏浏览题目 切换布局 作者 李祥 单位 湖北经济学院 定义如下点结构类型 typedef struct { double x, y; } POINT; 说明:x 和 y 分别为点的横坐标和纵坐标。 下面的程序输入两个点的坐标,求两点间的距离并输出。 #include <stdio.h> #include <math.h> typedef struct { double x, y; } POINT; int main() { POINT a, b; double s, p, q; scanf("%lf %lf", &a.x, &a.y); scanf("%lf %lf", &b.x, &b.y); /* 你提交的代码将被嵌在这里 */ printf("%g\n", s); return 0; }
可以使用勾股定理求解两点间的距离,具体实现如下:
```c
p = a.x - b.x;
q = a.y - b.y;
s = sqrt(p * p + q * q);
```
完整代码如下:
```c
#include <stdio.h>
#include <math.h>
typedef struct {
double x, y;
} POINT;
int main() {
POINT a, b;
double s, p, q;
scanf("%lf %lf", &a.x, &a.y);
scanf("%lf %lf", &b.x, &b.y);
p = a.x - b.x;
q = a.y - b.y;
s = sqrt(p * p + q * q);
printf("%g\n", s);
return 0;
}
```
注意:在输出距离时,可以使用 `%g` 格式符,它可以自动根据数值的大小选择使用 `%f` 或 `%e`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)