Flooded! UVA - 815 (sort排序)

错了好多遍,不知道为啥出错,如果有大神发现,请求指点!!!

附错误代码(错的不知道怎么回事): 

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 32 + 5;
int hblock[maxn*maxn];         /*elevation*/
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        if(n == 0 && m == 0)
            break;
//        memset(hblock, 0, sizeof(hblock));
        for(int i = 0; i < m * n; i++)
            scanf("%d", &hblock[i]);
        hblock[m*n] = 2147483647;
//        cout << hblock[m*n] << endl;
        sort(hblock, hblock + m*n);
        double v;
        double h;
        cin >> v;
        v = v / 100.0;            /*除以一个区的底面积,得到其高度*/
        double mark = 0;       /*标记被淹的极限*/
        for(int i = 1; i <= m * n; i++)
        {
            v += hblock[i-1];
            h = v / i;          /**淹完一个时的高度**/
            if(h < hblock[i])
            {
                mark = i;
                break;
            }
        }
        int cases = 0;
        printf("Region %d\n", ++cases);
        printf("Water level is %.2lf meters.\n", h);
        printf("%.2lf percent of the region is under water.\n", 100.0*mark/double(n*m));        /*double强制转换不可忘,不然会有大量损失*/

    }
    return 0;

}

正确代码:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define INF 0x7fffffff
int h[35 * 35], n, m;
double v, H, k;
int main()
{
    int cas = 0;
    while(scanf("%d%d", &n, &m), n)
    {
        n *= m;
        for(int i = 0; i < n; i++)
            scanf("%d", h + i);
        h[n] = INF, n++;
        sort(h, h + n);
        scanf("%lf", &v);
        v /= 100.0;
        for(int i = 1; i <= n; i++)
        {
            v += h[i - 1];
            H = v / i;
            if(H < h[i])
            {
                k = i;
                break;
            }
        }
        printf("Region %d\n", ++cas);
        printf("Water level is %.2lf meters.\n", H);
        printf("%.2lf percent of the region is under water.\n\n", 100.0 * k / (n - 1));
    }
    return 0;
}

 

posted @ 2019-04-25 15:48  DIY-Z  阅读(112)  评论(0编辑  收藏  举报