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代码:
题意:给你一个数字串,要你把它分成五部分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; }
相关文章推荐
- hdu 5938 Four Operations 贪心
- hdu 5938 Four Operations【贪心】
- HDU 5938 Four Operations(贪心+细节)——2016年中国大学生程序设计竞赛(杭州)
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations(枚举)
- HDU-5938 Four Operations(字符串处理)
- hdu 5938 Four Operations(乘法四则运算)
- hdu 5938 Four Operations
- HDU 5938 Four Operations(思维)
- hdu 5938 Four Operations(复杂的模拟题)
- hdu 5938 贪心 策略(2016年中国大学生程序设计竞赛(杭州))
- HDU 5938 Four Operations 想法题
- HDU 5938 && 2016CCPC杭州 F: Four Operations
- (2016年中国大学生程序设计竞赛(杭州)) HDU 5938 Four Operations 思维题 + 枚举
- HDU 5938 Four Operations (水题乱搞)
- HDU 5938 Four Operations(2016年中国大学生程序设计竞赛(杭州))
- HDU-5938-Four Operations(杭州CCPC F题)-水题
- 2016ccpc杭州赛 hdu 5938 F.Four Operations
- hdu 5938 Four Operations 模拟