Sicily 1050 Numbers & Letters

注意减法和除法可交换,除法判断`div`0


//#define LOCAL
#define DEBUG

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#if __cplusplus > 201103L
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define INF 0x3f3f3f3f
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
#define debug(...) printf( __VA_ARGS__) 
template<class T> void DISP(const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP(#x " to " #n, x, n)
#else
#define debug(...) 
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define car first
#define cdr second
#define PB push_back
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int _end_ = (b), i = (a); i <= _end_; ++i)
#define ROF(i, a, b) for (int _end_ = (b), i = (a); i >= _end_; --i)

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;

int64 fpm(int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin(T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax(T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
template <typename T> T gcd(T x, T y) {for (T t; x; t = x, x = y % x, y = t); return y; }

template<class edge> struct Graph
{
    vector<vector<edge> > adj;
    Graph(int n) {adj.clear (); adj.resize (n + 5);}
    Graph() {adj.clear (); }
    void resize(int n) {adj.resize (n + 5); }
    void add(int s, edge e){adj[s].push_back (e);}
    void del(int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
    vector<edge>& operator [](int t) {return adj[t];}
};

const int maxn = 6;
const int MOD = int(1e9) + 7;
const double EPS = 1E-9;
const double  PI = acos(-1.0); //M_PI;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int ans;
int num[maxn], target;
void dfs(int s[], int len);
int main()
{
#ifdef LOCAL 
    freopen("data.in","r",stdin);  
#endif
    int nn;
    cin>>nn;
    while(nn--) {
        for (int i=1; i<=5; i++) {
            cin>>num[i];
        }
        cin>>target;
        ans = -1 * INF;
        for (int i=1; i<=5; i++) {
            if (ans < num[i] && num[i] <= target) {
                ans = num[i];
            }
        }
        dfs(num, 5);
        cout<<ans<<endl;
    }
}
void dfs(int s[], int len) {
    if (ans == target) return;
    if (len == 1) {
        return;
    }
    for (int i=1; i<=len; i++) {
        for (int j=i+1; j<=len; j++) {
            int t[6], cnt=0;
            for (int k=1; k<=len; k++) {
                if (k!=i && k!=j) {
                    t[++cnt] =s[k];
                }
            }
            int tmp = s[i] *s[j];
            if (tmp > ans && tmp <= target) ans = tmp;
            t[++cnt] = tmp;
            dfs(t, cnt);
            tmp = s[i] + s[j];
            if (tmp > ans && tmp <= target) ans = tmp;
            t[cnt] = tmp;
            dfs(t, cnt);
            // - (1)
            tmp = s[i] - s[j];
            if (tmp > ans && tmp <= target) ans = tmp;
            t[cnt] = tmp;
            dfs(t, cnt);
            // `sub` (2)
            tmp = s[j] - s[i];
            if (tmp > ans && tmp <= target) ans = tmp;
            t[cnt] = tmp;
            dfs(t, cnt);
            // `div`
            int a = min(s[i],s[j]), b= max(s[i],s[j]);
            if (a != 0 && b % a == 0) {
                tmp = b / a;
                if (tmp > ans && tmp <= target) ans = tmp;
                t[cnt] = tmp;
                dfs(t, cnt);
            }
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值