5 solutions
- 1
Information
- ID
- 36
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 2036
- Accepted
- 400
- Uploaded By
# π的精度要求很高,记不到的话可以用求π的公式
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323
int main()
{
int r;
double s;
scanf("%d",&r);
s = pow(r,2)*PI;
printf("%.7lf",s);
return 0;
}
这道题难在求Π的精度问题;
刚开始一直用Π=1-(1/3)+(1/5)....的公式计算精度,但是我写的代码直接超时,要不然就是精度不够。。。
所以以后求Π使用第二种方法:
通过三角函数:atan(1.0)=Π/4,结果乘4得Π!!
#include <stdio.h>
#include <algorithm>
#include <math.h>
int main() {
int x,y;
double pi=atan(1)*4,k=1,q=1.0/k;
int tmp=-1;
scanf("%d",&x);
double s=pi*x*x;
printf("%.7lf\n",s);
return 0;
}
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.