JXUFE紫书第四章习题4-10

本文介绍了一种用于模拟洪水在特定地形中扩散情况的算法。该算法能够估算水位高度及淹没区域的比例,适用于房屋购买者评估洪水风险。通过输入地形高度数据和预估洪水总量,算法能准确预测洪水对地区的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文题目:

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;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值