C語言:求π(Π,pi)


1835: 圓的面積

 

 

 

 

本題的關鍵在於如何求π;

今天先給給大家介紹一種針對本題的方法——利用反三角函數求π。

在高數中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\n",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\n",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\n",s);
25 }

2019-12-06


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM