题意:给一个只含'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;
}