怎么说呢,果然题目做多了思路也就多了,看到这一题,马上想到2分水的体积,写了一下wa了
错在少考虑了负数的情况所以改了一下二分搜素的上下界就a掉了
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-8;
int sgn(double x)
{
if(x<0)
return -1;
if(fabs(x)<eps)
return 0;
else
return 1;
}
int n,m;
double map[50][50];
double judge(double mid)
{
double ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(sgn(mid-map[i][j])==1)
{
ans+=(mid-map[i][j])*10*10;
}
}
}
return ans;
}
int main()
{
int fg=1;
double max1=1e18;
double min1=-1e18;
while(cin>>n>>m&&n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>map[i][j];
max1=min(map[i][j],max1);
min1=max(map[i][j],min1);
}
}
double ans;
cin>>ans;
double t;
//t=ans/(10*10*n*m);
double l=max1;
double r=ans+min1;
while(sgn(r-l)>0)
{
double mid;
mid=(l+r)/2;
if(sgn(judge(mid)-ans)==1)
{
r=mid-eps;
}
else
{
l=mid+eps;
}
}
printf("Region %d\n",fg++);
printf("Water level is %.2lf meters.\n",l);
int ans2=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(sgn(l-map[i][j])==1)
{
ans2++;
}
}
}
printf("%.2lf percent of the region is under water.\n\n",(ans2*1.0)/(n*m*1.0)*100);
}
return 0;
}