[HDOJ5938]Four Operations(暴力,DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938

题意:给出一个长度最大是2020的数字串, 你要把数字串划分成55段, 依次填入’+’, ’-’, ’*’, ’/’, 问能得到的最大结果。

想让这个结果最大,那尽可能让减号左边的数最大,只需要关心左边的第一位是一个数还是最后一位是一个数,后面的枚举*和/的位置就行了。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const int maxn = 22;
 6 LL ret, dret;
 7 int n;
 8 char s[maxn];
 9 
10 void dfs(int pos, int cnt, LL c, LL d, LL e) {
11     if(pos >= n) return;
12     if(cnt == 3) {
13         dret = min(dret, c * d / e);
14         return;
15     }
16     if(cnt == 0) {
17         LL tmp = 0;
18         for(int i = pos; i < n; i++) {
19             tmp *= 10; tmp = tmp + s[i] - '0';
20             dfs(i+1,cnt+1, tmp,d,e);
21         }
22     }
23     if(cnt == 1) {
24         LL tmp = 0;
25         for(int i = pos; i < n; i++) {
26             tmp *= 10; tmp = tmp + s[i] - '0';
27             dfs(i+1,cnt+1,c,tmp,e);
28         }
29 
30     }
31     if(cnt == 2) {
32         LL tmp = 0;
33         for(int i = pos; i < n; i++) {
34             tmp *= 10; tmp = tmp + s[i] - '0';
35         }
36         dfs(-1,cnt+1,c,d,tmp);
37     }
38 }
39 
40 int main() {
41     // freopen("in", "r", stdin);
42     int T, _ = 1;
43     scanf("%d", &T);
44     while(T--) {
45         scanf("%s", s);
46         n = strlen(s);
47         ret = -((LL)1 << 40);
48         for(int i = 1; i < n-3; i++) {
49             LL x = 0;
50             LL y = 0;
51             LL s1 = 0, s2 = 0, ss = 0;
52             int j = 0;
53             while(j < i) {
54                 x *= 10;
55                 x = x + s[j++] - '0';
56             }
57             y = s[i] - '0';
58             s1 = x + y;
59             j = 1;
60             x = s[0] - '0'; y = 0;
61             while(j <= i) {
62                 y *= 10;
63                 y = y + s[j++] - '0';
64             }
65             s2 = x + y;
66             ss = max(s1, s2);
67             dret = (LL)1 << 40;
68             dfs(i+1, 0, 0, 0, 0);
69             ret = max(ret, ss-dret);
70         }
71         printf("Case #%d: ", _++);
72         printf("%I64d\n", ret);
73     }
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/kirai/p/6013183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值