zoukankan      html  css  js  c++  java
  • 【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
    ", ++t, j-ans, j);
    129     }
    130     
    131     return 0;
    132 }
  • 相关阅读:
    省选模拟27
    省选模拟26
    省选模拟25
    省选模拟23
    PHP 各个框架的优缺点(超详细)
    windows 下安装中文版Git
    支付宝APP支付里设置应用网关和授权回调地址是不必填的
    linux服务器下安装phpstudy 如何命令行进入mysql
    js金钱转大写
    多个 (li) 标签如何获取获取选中的里面的某个特定值??
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4306736.html
Copyright © 2011-2022 走看看