题意:来自:https://blog.csdn.net/lecholin/article/details/70186673
思路:
①数组存每个网格的高度,然后排序,做题时想象为上面的柱状图。
②注意对淹没的处理。
③注意每个输出都有空行。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
int main()
{
//FIN;
//FOUT;
int m, n;
int data[],kase=;
double level, percentage, high = ,sum;
while (scanf("%d%d",&m,&n)==&&n!=&&m!=)
{
kase++;
for (int i = ; i < m*n; i++)
{
scanf("%d", &data[i]);
}
high = ;
scanf("%lf", &sum);
sum /= ;//正方形为10*10,化简一下sum
//排序
for (int i = ; i < m*n; i++)
{
for (int j = i + ; j < m*n; j++)
{
if (data[i] > data[j])
{
int ex = data[i];
data[i] = data[j];
data[j] = ex;
}
}
}
//data[m*n] = 100000;//最大的边界
//输出
/*printf("排序后:\n");
for (int i = 0; i < m*n; i++)
{
printf("%-4d", data[i]);
if ((i + 1) % m == 0)
printf("\n");
}*/
for (int i = ; i < n*m; i++)
{
if (sum != - && i == m*n - )//找到最后一个说明淹没了
{
high += (sum*1.0) / (i + );
high += data[];
int cntDown = (i + );
percentage = (cntDown*1.0) / (m*n) * ;
sum = -;
break;
}
if ((data[i + ] - data[i])*(i+)< sum)//小就继续往上面走
{
sum -= (data[i + ] - data[i])*(i+);
high += data[i+]-data[i];
}
else
{
high += (sum*1.0) / (i+);
high += data[];
int cntDown =(i+);
percentage = (cntDown*1.0) / (m*n)*;
sum = -;
break;
} }
printf("Region %d\n", kase);
printf("Water level is %.2f meters.\n",high);
printf("%.2f percent of the region is under water.\n\n",percentage);
}
return ;
}