有一個n*m(1<=n,m<=30)的網格,每一個格子都是邊長爲10米的正方形,網格四周是無限大的牆壁。
輸入每一個格子的海拔高度,以及網格內雨水的整體積,輸出水位的海拔高度以及有多少百分比的區域有水
3 3 //3行3列的網格
25 37 45 //格子本來海拔高度
51 12 34
94 83 27
10000 //要注入的水總量
0 0 //表示結束輸入
Region 1
Water level is 46.67 meters.
66.67 percent of the region is under water.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define M 30
#define N 30
float Area_M[M][N];
float Area_W[M][N];
int m, n;
float c;
void getWaterHighInfo()
{
int reg_c;
float w_min,H_all = 0.0;
while (c>0)
{
w_min = Area_M[0][0] + Area_W[0][0],reg_c = 0; //設置最小水平初始值
//先找到最小水平面值
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (w_min>Area_M[i][j] + Area_W[i][j])
w_min = Area_M[i][j] + Area_W[i][j];
//向其中放水
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
if (Area_M[i][j] + Area_W[i][j] == w_min)
{
Area_W[i][j] += 0.01;
reg_c++;
}
}
c -= 0.01 * 10 * 10*reg_c; //減去相應水量
}
//打印水平高度
//先找到一個有水的方格
reg_c = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
if (Area_W[i][j] != 0)
{
reg_c++;
H_all += Area_W[i][j]+Area_M[i][j]; //由於float含有偏差,咱們單獨輸出一個數據可能有偏差,因此咱們將全部數據獲取,取平均值來減小偏差
}
}
//打印百分比
printf("Water level is %.2f meters.\n", H_all / reg_c);
printf("%.2f percent of the region is under water.\n", reg_c*100.0 / (m*n));
}
void main()
{
FILE* fp = freopen("data10.in", "r", stdin);
freopen("data10.out", "w", stdout);
int count = 1;
while (!feof(fp))
{
scanf("%d %d", &m, &n);
getchar();
if (m == 0)
break;
printf("Region %d\n", count++);
//獲取山體高度
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
scanf("%f", &Area_M[i][j]);
getchar();
}
scanf("%f", &c);
getchar();
memset(Area_W, 0, sizeof(Area_W));
getWaterHighInfo();
}
freopen("CON", "r", stdin);
freopen("CON", "w", stdout);
}