UVa815_Flooded!

时间:2022-07-14 14:49:48
 #include <iostream>
//#include <fstream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int maxn = * + ;
const int space = * ; int main()
{
//ofstream out("out.txt");
int m, n, kase = , volume; //降水总量
double eleva[maxn], height; //每个格子的海拔高度, 输入的高度
double percnt = ; //有多少百分比的区域
while (cin >> m >> n)
{
memset(eleva, , sizeof(eleva));
if (!m && !n) break;
for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
cin >> height;
eleva[i * n + j] = height;
}
}
cin >> volume;
sort(eleva, eleva + m*n); //排序 int k, comput = ; //计算已经淹没地区的水量
for (k = ; k < m*n; k++)
{
int tmp = comput + (eleva[k] - eleva[k - ]) * space * k;
if (tmp >= volume)
break;
comput = tmp;
}
double water = (volume - comput) / (double)(k * space) + eleva[k - ]; //最终水平面高度是 = 当前被淹没的最高海拔 + 水面比当前海拔高出的高度 cout << "Region " << ++kase << "\n";
cout << fixed << setprecision()
<< "Water level is " << water << " meters.\n"; double total = m*n;
percnt = (k*100.0 / total) ; //被淹没区域比例 = 当前比水平面低的海拔数 / 区域总数 cout << fixed << setprecision()
<< percnt << " percent of the region is under water.\n\n";
} return ;
}
UVa815_Flooded!如图,将海拔按高矮排序好(先排序好,比较容易计算已经淹没地区的水量),由于是水往地处流,如果水量如图,淹没区域如图中阴影地区。

相关文章