赞
踩
没难度的二分题,若果WA要多多留意是不是精度问题,因为算法思路是很明确的,精度才是最头疼的(参考http://blog.csdn.net/lyy289065406/article/details/6648565)
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #define PI acos(-1)
- #define max(a,b) ((a)>(b)?(a):(b))
- #define N 10005
- int n,m,T;
- int s[N];
- int main(){
- freopen("a.txt","r",stdin);
- scanf("%d",&T);
- while(T--){
- int i,num;
- double low,high,mid;
- low = high = 0;
- scanf("%d %d",&n,&m);
- for(i = 0;i<n;i++){
- scanf("%d",&s[i]);
- s[i] *= s[i];//取平方
- high = max(high,1. * s[i]);
- }
- m++;//注意自己也要吃一份耶
- while(high-low > 0.000001){//注意实数二分的判断条件
- mid = (low+high)/2;
- num = 0;
- for(i = 0;i<n;i++)
- num += (int)(s[i]/mid);
- if(num >= m)
- low = mid;//直接等于mid是为了精度考虑
- else
- high = mid;
- }
- printf("%.4lf\n",PI*mid);
- }
- return 0;
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。