JXUFE紫书第四章习题4-10

xiaoxiao2021-02-28  6

英文题目:

To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients withthe elevation of each 10-meter by 10-meter square of land in regions where homes may be purchased.Water from rain, melting snow, and burst water mains will collect first in those squares with thelowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we alsoassume that storm sewers enable water from high-elevation squares in valleys (completely enclosed bystill higher elevation squares) to drain to lower elevation squares, and that water will not be absorbedby the land.

From weather data archives, we know the typical volume of water that collects in a region. Asprospective homebuyers, we wish to know the elevation of the water after it has collected in lowlyingsquares, and also the percentage of the region’s area that is completely submerged (that is, thepercentage of 10-meter squares whose elevation is strictly less than the water level). You are to writethe program that provides these results.

Input

The input consists of a sequence of region descriptions. Each begins with a pair of integers, m andn, each less than 30, giving the dimensions of the rectangular region in 10-meter units. Immediatelyfollowing are m lines of n integers giving the elevations of the squares in row-major order. Elevationsare given in meters, with positive and negative numbers representing elevations above and below sealevel, respectively. The final value in each region description is an integer that indicates the number ofcubic meters of water that will collect in the region. A pair of zeroes follows the description of the lastregion.

Output

For each region, display the region number (1, 2, . . . ), the water level (in meters above or below sealevel) and the percentage of the region’s area under water, each on a separate line. The water leveland percentage of the region’s area under water are to be displayed accurate to two fractional digits.Follow the output for each region with a blank line.

Sample Input

3 3

25 37 45

51 12 34

94 83 27

10000

0 0

Sample Output

Region 1

Water level is 46.67 meters.

66.67 percent of the region is under water.

中文题意:

一个n*m的方格区域,共有n*m个方格,每个方格是边长为10米的正方形,整个区域的外围是无限高的高墙,给出这n*m个方格的初始高度,和洪水的总体积,计算灌入洪水后这个方格区域的水面高度,以及洪水淹没比例。

分析:

淹没肯定是从高度最低的方格就开始的,所以先将n*m个方格从小到大排序。如果洪水要想淹没下一个方格,那么剩余洪水的体积必须>=已经淹没方格的面积*已经淹没方格的数量,这样水面才能整体拔高。如果满足不了这个条件,只能在原来的高度上均匀拔高了。

需要注意的是:①由于每个方格都是10*10的正方形面积,可以先将洪水体积/100,这样只需考虑高度。②洪水有可能淹没整个地区

代码参考:

#include <stdio.h>  #include <string.h>  #define maxn 900  int i,j,cases=0,n,m,tot,t;  int a[maxn];  double sum,high,per;  int main()  {      while(~scanf("%d%d",&n,&m)&&n)      {          for(i=1;i<=n*m;i++)              scanf("%d",&a[i]);          scanf("%lf",&sum);          sum/=100;          for(i=1;i<n*m;i++)//选择排序          {              t=i;              for(j=i+1;j<=n*m;j++)                  if(a[j]<a[t]) t=j;              if(t!=i)              {                  int tmp=a[i];                  a[i]=a[t];                  a[t]=tmp;              }          }          for(i=2;i<=n*m;i++)          {              tot=(a[i]-a[i-1])*(i-1);              if(sum>=tot)              {                  sum-=tot;              }              else              {                  high=a[i-1]+sum/(i-1);                  sum=0;                  per=(i-1)*1.0/n/m;                  break;              }          }          if(sum)        {              high=a[n*m]+sum/n/m;              per=1;          }          printf("Region %d\n",++cases);          printf("Water level is %.2lf meters.\n",high);          printf("%.2lf percent of the region is under water.\n\n",per*100);      }      return 0;  }  

转载请注明原文地址: https://www.6miu.com/read-2650126.html

最新回复(0)