hdu5938Four Operations

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938

题意:给一个只含'1'~'9'的字符串,要求按顺序插入'+','-','*','/',求运算结果中的最大值。

分析:要求运行结果最大,设为a+b-c*d/e很明显我们能观察到要让前面a+b尽量大即a<10或b<10以及cde的时候e要尽量大c*d尽量小即c<10和d<10,那么我们只需要枚举-号的位置,我们就能确定运算答案。

代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-8;
const int N=1e2+10;
const int M=2e5+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
ll a,b,c,ans;
char s[25];
int main()
{
    int i,j,T,ca,len;
    scanf("%d", &T);
    for (ca=1;ca<=T;ca++) {
        scanf("%s", s);
        len=strlen(s);ans=-MOD;
        for (i=1;i<len-3;i++) {
            a=b=c=0ll;
            for (j=0;j<i;j++) a=a*10ll+s[j]-'0';
            a+=s[i]-'0';
            for (j=1;j<=i;j++) c=c*10ll+s[j]-'0';
            c+=s[0]-'0';
            a=max(a,c);c=0ll;
            b=(s[i+1]-'0')*(s[i+2]-'0');
            for (j=i+3;j<len;j++) c=c*10ll+s[j]-'0';
            b/=c;
            ans=max(ans,a-b);
        }
        printf("Case #%d: %lld\n", ca, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值