zoukankan      html  css  js  c++  java
  • HDU 5938 Four Operations(乱搞)题解

    题意:把'+', '-', '*' 和'/'按顺序插入任意两数字间隔,使得操作得到后计算后最大。

    思路:没想到是个水题,打的时候想得太复杂了。这道题其实只要考虑*和/。显然我们要把a*b/c弄到最小。那么ab只有一位,c可能有两位。m+n-a*b/c最大,那么mn和最大,那么就是-号前面分割最大和。那么就是max(第一位+后面,前面+最后一位)。

    代码:

    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    typedef long long ll;
    const int maxn = 100000 + 10;
    const int seed = 131;
    const ll MOD = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    char a[35];
    int num1, num2;
    ll pre1, pre2;
    bool check(int len){
        num1 = (a[len - 2] - '0') * (a[len - 1] - '0') / (a[len] - '0');
        num2 = (a[len - 3] - '0') * (a[len - 2] - '0') / ((a[len - 1] - '0') * 10 + (a[len] - '0'));
        if(len == 5) return false;
        if(num1 > num2) return true;
        else return false;
    }
    ll big(int len){
        ll num = 0, Max;
        for(int i = 1; i <= len - 1; i++){
            num = num * 10 + (a[i] - '0');
        }
        Max = num + (a[len] - '0');
        num = 0;
        for(int i = 2; i <= len; i++){
            num = num * 10 + (a[i] - '0');
        }
        num += (a[1] - '0');
        Max = max(Max, num);
        return Max;
    }
    int main(){
        int T, Case = 1;
        ll ans;
        scanf("%d", &T);
        while(T--){
            ans = -INF;
            scanf("%s", a + 1);
            int len = strlen(a + 1);
            bool two = check(len);
            if(two){
                ans = big(len - 3) - num1;
                ans = max(big(len - 4) - num2, ans);
            }
            else{
                ans = big(len - 3) - num1;
            }
            printf("Case #%d: %lld
    ", Case++, ans);
        }
        return 0;
    }
  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9807849.html
Copyright © 2011-2022 走看看