UVa 10576 - Y2K Accounting Bug

题目

已知一个公司的12个月的账目,每个月有两种状态盈利s或损失d。现在只知道每连续五个月的收支状态都是相同的亏损(1~5,2~6,..,8~12的收支总和相同),问公司最多可能有多大盈利。

分析

枚举、位运算。
设1~5月的收支分别为a、b、c、d、e,那么6~12一定为a、b、c、d、e、a、b。
这时我们枚举a~e的值分别取s和d( 25=32 2 5 = 32 种),然后找到满足a+b+c+d+e小于零的,a+b+c+d+e+a+b+c+d+e+a+b的最大值即可。

说明

如果最大的收支都不超过0,则输出Deficit。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int s, d;
    while (~scanf("%d%d", &s, &d)) {
        int max = -1 - 12*d;
        for (int i = 0; i < 32; ++ i) {
            int sum_of_five = 0, sum_of_two = 0;
            for (int j = 0; j < 5; ++ j) {
                if ((1<<j)&i) {
                    sum_of_five -= d;
                    if (j < 2) {
                        sum_of_two -= d;
                    }
                }else {
                    sum_of_five += s;
                    if (j < 2) {
                        sum_of_two += s;
                    }
                }
            }
            int sum_of_twelve = 2*sum_of_five + sum_of_two;
            if (max < sum_of_twelve && sum_of_five < 0) {
                max = sum_of_twelve;
            }
        }

        if (max < 0) {
            printf("Deficit\n");
        }else {
            printf("%d\n", max);
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值