HDU 5938 Four Operations 贪心

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5938
题意:给你一个数字串,要你把它分成五部分ABCDE,然后计算A+B-C*D/E可能的最大值是多少?
解题思路:枚举负号的位置,因为要使整个值最大,C*D应该最小,所以C和D都只取一位。A+B的值最大需要使A或B的位数尽可能大,即A一位,B为到负号前的所有位或者B为符号前一位,A为开头到负号前一位位置,两种情况取最大值。由于C和D都确定了E也随之确定了。所以总的来说就是枚举一下负号的位置,然后判断一下A+B的最大值就可以了。
注意要用long long。
AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[25];
typedef long long ll;
ll num[25];
const ll INF = 1e18;
int main()
{
    int t;
    ll ans1,ans2,ans,tmp1,tmp2,tmp3;
    scanf("%d", &t);
    for (int m = 1; m <= t; m++)
    {
        scanf("%s", str);
        int len = strlen(str);
        ans = -INF;
        for (int i = 0; i < len; i++)
            num[i] = str[i] - '0';
        for (int i = 1; i <= len-4; i++)//枚举负号位置
        {
            ans1 = 0;
            ans2 = 0;
            for (int j = 0; j < i; j++)
                ans1 = ans1 * 10ll + num[j];
            ans1 += num[i];
            for (int j = 1; j <= i; j++)
                ans2 = ans2 * 10ll + num[j];
            ans2 += num[0];
            ans1 = max(ans1,ans2);
            tmp1 = num[i+1];
            tmp2 = num[i+2];
            tmp3 = 0;
            for (int j = i+3; j < len; j++)
                tmp3 = tmp3 * 10 + num[j];
            ans = max(ans,ans1-tmp1*tmp2/tmp3);
        }
        printf("Case #%d: %I64d\n",m,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值