HDU 5938(字符串+贪心)

题意:给你一个字符串,把它分成四块,按顺序把符号+ - * /放上去,问能得到的最大数是多少。

思路:因为有-,所以*和/后的结果要尽可能小,一位数*一位数最大只能是两位数,所以除数就可能为一位数或两位数,如果是三位数,那么会产生小数,从而浪费了前面的加和。还有就是前面相加的两个数可能是所有数+最后一位(相对而言)或第一位+后面的全部。四种情况比较一下大小即可。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int a[30];
char s[30];
ll solve(int l,int r)
{
    ll ans=0;
    for(int i=l;i<=r;i++)
    {
        ans=ans*10+a[i];
    }
    return ans;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int t,cas=0;
    string s;
    cin>>t;
    while(t--)
    {

        cin>>s;
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            a[i]=s[i]-'0';
        }
        ll ans=a[0]+solve(1,len-4)-a[len-3]*a[len-2]/a[len-1];
        ans=max(ans,solve(0,len-5)+a[len-4]-a[len-3]*a[len-2]/a[len-1]);
        if(len>5)
        {
            int div=solve(len-2,len-1);
            ans=max(ans,a[0]+solve(1,len-5)-a[len-4]*a[len-3]/div);
            ans=max(ans,solve(0,len-6)+a[len-5]-a[len-4]*a[len-3]/div);
        }
        printf("Case #%d: %lld\n",++cas,ans);
    }
    return 0;
}

 

posted @ 2018-08-25 01:18  MCQ  阅读(136)  评论(0编辑  收藏  举报