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 <cstdio>
#include <vector>
#include <algorithm>
// #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<int> 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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值