您的位置:首页 > 运维架构

HDU 5938 Four Operations 贪心

2016-12-01 01:32 309 查看
题目传送门: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: