【HDOJ】3325 Arithmetically Challenged

简单DFS。

  1 /* 3325 */
  2 #include <iostream>
  3 #include <set>
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 #define MAXN 100005
 10 #define INF 0x3f3f3f3f
 11 
 12 int buf[MAXN];
 13 int a[4];
 14 set<int> S;
 15 set<int>::iterator iter;
 16 
 17 int cal(int a, int b, int op) {
 18     if (op == 0)    return a+b;
 19     if (op == 1)    return a-b;
 20     if (op == 2)    return a*b;
 21     if (op == 3)    return (b==0||a%b) ? INF:a/b;
 22     return INF;
 23 }
 24 
 25 void f() {
 26     int i, j, k;
 27     int tmp1, tmp2;
 28     int ans;
 29     
 30     //((AB)C)D (AB) (CD)
 31     for (i=0; i<4; ++i) {
 32         tmp1 = cal(a[0], a[1], i);
 33         if (tmp1 == INF)
 34             continue;
 35         for (j=0; j<4; ++j) {
 36             tmp2 = cal(tmp1, a[2], j);
 37             if (tmp2 != INF) {
 38                 for (k=0; k<4; ++k) {
 39                     ans = cal(tmp2, a[3], k);
 40                     if (ans != INF)
 41                         S.insert(ans);
 42                 }
 43             }
 44             tmp2 = cal(a[2], a[3], j);
 45             if (tmp2 != INF) {
 46                 for (k=0; k<4; ++k) {
 47                     ans = cal(tmp1, tmp2, k);
 48                     if (ans != INF)
 49                         S.insert(ans);
 50                 }
 51             }
 52         }
 53     }
 54     
 55     // (A(BC))D A((BC)D)
 56     for (i=0; i<4; ++i) {
 57         tmp1 = cal(a[1], a[2], i);
 58         if (tmp1 == INF)
 59             continue;
 60         for (j=0; j<4; ++j) {
 61             tmp2 = cal(a[0], tmp1, j);
 62             if (tmp2 != INF) {
 63                 for (k=0; k<4; ++k) {
 64                     ans = cal(tmp2, a[3], k);
 65                     if (ans != INF)
 66                         S.insert(ans);
 67                 }
 68             }
 69             tmp2 = cal(tmp1, a[3], j);
 70             if (tmp2 != INF) {
 71                 for (k=0; k<4; ++k) {
 72                     ans = cal(a[0], tmp2, k);
 73                     if (ans != INF)
 74                         S.insert(ans);
 75                 }
 76             }
 77         }
 78     }
 79     
 80     // A(B(CD))
 81     for (i=0; i<4; ++i) {
 82         tmp1 = cal(a[2], a[3], i);
 83         if (tmp1 == INF)
 84             continue;
 85         for (j=0; j<4; ++j) {
 86             tmp2 = cal(a[1], tmp1, j);
 87             if (tmp2 != INF) {
 88                 for (k=0; k<4; ++k) {
 89                     ans = cal(a[0], tmp2, k);
 90                     if (ans != INF)
 91                         S.insert(ans);
 92                 }
 93             }
 94         }
 95     }
 96 }
 97 
 98 int main() {
 99     int t = 0, n;
100     int i, j, k;
101     int ans;
102     
103     #ifndef ONLINE_JUDGE
104         freopen("data.in", "r", stdin);
105         freopen("data.out", "w", stdout);
106     #endif
107     
108     while (scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF && (a[0]+a[1]+a[2]+a[3])) {
109         S.clear();
110         sort(a, a+4);
111         do {
112             f();
113         } while (next_permutation(a, a+4));
114         n =0;
115         for (iter=S.begin(); iter!=S.end(); ++iter)
116             buf[n++] = *iter;
117         ans = k = 0;
118         for (i=1; i<n; ++i) {
119             if (buf[i]==buf[i-1]+1)
120                 ++k;
121             else
122                 k = 0;
123             if (ans <= k) {
124                 ans = k;
125                 j = buf[i];
126             }
127         }
128         printf("Case %d: %d to %d\n", ++t, j-ans, j);
129     }
130     
131     return 0;
132 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4306736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值