815 - Flooded! (UVA)

题目链接如下:

Online Judge

这道题要求“Follow the output for each region with a blank line.”,而不是region之间加空行,我在这个点上卡了很久……

然后我有点把题复杂化了,其实不用算currArea那么复杂,直接用一个for循环,循环到哪就有几块地被淹了,挺简单的....下面这个代码写得比较好。

https://www.cnblogs.com/zyb993963526/p/6297474.html

我的(复杂)代码如下:

#include 
#include 
#include 
// #define debug

int m, n, tot, currArea, pivot, kase = 0;
double waterLevel;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while(scanf("%d %d", &m, &n) == 2 && m && n){
        std::vector vec(m * n);
        for(int i = 0; i < m * n; ++i){
            scanf("%d", &vec[i]);
        }
        sort(vec.begin(), vec.end());
        scanf("%d", &tot);
        currArea = 0;
        pivot = -1;
        waterLevel = vec[0];
        while(tot){
            do{
                pivot++;
                currArea += 100;
            } while(pivot < vec.size() - 1 && vec[pivot + 1] == vec[pivot]);
            if(pivot == vec.size() - 1 || tot * 1.0 / currArea <= vec[pivot + 1] - vec[pivot]){
                waterLevel = vec[pivot] + tot * 1.0 / currArea;
                break;
            }
            tot -= currArea * (vec[pivot + 1] - vec[pivot]);
        }
        printf("Region %d\nWater level is %.2f meters.\n%.2f percent of the region is under water.\n\n", ++kase, waterLevel, (pivot + 1) * 100.0 / m / n);
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)