C语言:求π(Π,pi)

1835: 圆的面积

C语言:求π(Π,pi)

本题的关键在于如何求π;

今天先给给大家介绍一种针对本题的方法——利用反三角函数求π。

在高数中arcsin(0)=arccos(1)=π,不过编译器中并没有arcsin和arccos函数,有与其对应的函数分别为asin和acos,这两个函数是在math.h头文件中,需要在一开始写上这个头文件。以下贴上本题的完整代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 int main()
 4 {
 5     const double pi=acos(1.0);
 6     double r,s;
 7     scanf("%lf",&r);
 8     s=pi*pow(r,2);
 9     printf("%.7f
",s);
10 }

此外,介绍一些其他的方法:

1、常量定义

可以用const或者采用#define宏定义进行预处理,给出一个较精确的值3.14159265625‬,大家也可以通过计算22/7得到,

 1 //const定义
 2 #include <stdio.h>
 3 #include <math.h>
 4 
 5 int main()
 6 {
 7     const double pi=3.14;
 8     double r,s;
 9     scanf("%lf",&r);
10     s=pi*pow(r,2);
11     printf("%.7f
",s);
12 }
13 
14 
15 //宏定义
16 #include <stdio.h>
17 #include <math.h>
18 #define pi 3.14
19 int main()
20 {
21     double r,s;
22     scanf("%lf",&r);
23     s=pi*pow(r,2);
24     printf("%.7f
",s);
25 }

2019-12-06


题目来自:https://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1835