题目链接:http://acmicpc.openjudge.cn/practice/002/ 坑: 1.pi的精度,精度已经到小数点后9位,还是WA,下次要用cmath库中的acos(-1) 2.r和l的距离,精度过高,如0.0000000001,会超时。用0.000001就行了。 #include <iostream> #include <iomanip> using namespace std; double r[10001]; int n,f; bool check(double v); int main(){ cin>>n>>f; f = f+1; double maxx=0; for(int i=1;i<=n;i++){ cin>>r[i]; r[i] = 3.141592653589793*r[i]*r[i]; //把pi的精度降低为3.1415926535会WA,下次用<cmath>里的pi = acos(-1)!!! if(r[i]>maxx){ maxx = r[i]; } } double mid=0,l=0,r = maxx; while (r-l>0.000001){ //把精度设置过高,如:0.000000001,会超时 mid = (l+r)/2; if(check(mid)){ l = mid; } else{ r = mid; } } cout<<setiosflags(ios::fixed)<<setprecision(3); cout<<l; //l==r,注意:此题要得到的不是mid return 0; } bool check(double v){ int sum=0; for(int i=1;i<=n;i++){ sum = sum+int(r[i]/v); if(sum>=f){ return true; } } return false; }