POJ 2586-暴力水题

本文解析了一个关于12个月盈亏分析的算法题目,通过对比盈亏比例,确定了在不同条件下的最终盈亏情况。代码实现使用C++,展示了如何根据不同盈亏比计算总盈亏。

题目:考验英语和语文能力。

借鉴于:不知道是谁写的不太详细的题目解析;详细版我写在后面了。

>错误分析:不想读题。题目出的有点不够清楚。

题目拆解:

1)12个月的盈亏分析

2)每一个月要么是盈要么是亏,不同月份盈利额不会改变;亏损额也不会改变。(照应INPUT)

3)每连续的5个月一定是亏损的

4)求12个月总的算起来,盈利多少?如果亏损,亏损最少为多少?

num  需要多少个deficit才能使得5个月中deficit>surplus
 

123455554321   重复次数 ---line1
123456789012   月份序号 ---line2
-----  -----
 ----------
  ---++---
   -++++-
543214512345    按照亏损次数进行的分类 ---line3
>= 5
< 5
1
2
3
4

如上图,如果1/5的亏损,那么line3的所有“1”就是必须亏损的月份;2/5,那么就是所有的“2”,有四次。

 

代码:

#include <iostream>

using namespace std;

int main(){
    int surplus, deficit;
    int cnt;
    while(cin >> surplus >> deficit){
        cnt = 0;
        if(deficit > surplus*4){
            cnt = surplus*10 - deficit*2;
        }
        else if(2*deficit > surplus*3){
            cnt = surplus*8 - deficit*4;
        }
        else if(3*deficit > surplus*2){
            cnt = surplus*6 - deficit*6;
        }
        else if(4*deficit > surplus){
            cnt = surplus*3 - deficit*9;
        }
        else {//5 deficits
            cnt = -1;
        }

       if(cnt < 0) cout << "Deficit\n";
       else cout << cnt << '\n';
    }

    return 0;
}
// num is 需要多少个deficit才能使得5个月中deficit>surplus
// 123455554321
// 123456789012
// -----  -----
//  ----------
//   ---++---
//    -++++-
// 543214512345
// >= 5
// < 5
// 1
// 2
// 3
// 4

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值