Little Ruins is a studious boy, recently he learned the four operations!
Now he want to use four operations to generate a number, he takes a string which only contains digits '1' - '9', and split it into 55 intervals and add the four operations '+', '-', '*' and '/' in order, then calculate the result(/ used as integer division).
Now please help him to get the largest result.
InputFirst line contains an integer
TT, which indicates the number of test cases.
Now he want to use four operations to generate a number, he takes a string which only contains digits '1' - '9', and split it into 55 intervals and add the four operations '+', '-', '*' and '/' in order, then calculate the result(/ used as integer division).
Now please help him to get the largest result.
Every test contains one line with a string only contains digits '1'- '9'.
Limits
1≤T≤1051≤T≤105
5≤length of string≤205≤length of string≤20OutputFor every test case, you should output 'Case #x: y', where x indicates the case number and counts from 1 and y is the result.Sample Input
1 12345Sample Output
Case #1: 1
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
int main() {
int T;
scanf("%d",&T);
string s;
for(int k=1; k<=T; k++) {
cin>>s;
int n=s.size();
long long a=0,b=0,c=0,d=0,e=0,ans=-99999999;
for(int i=1;i<=3;i++)
{
for(int j=i-1;j>=0;j--)
{
e=e*10+s[n-1-j]-'0';
}
d=s[n-1-i]-'0';
c=s[n-2-i]-'0';
b=s[n-3-i]-'0';
for(int j=0;j<=n-4-i;j++)
{
a=a*10+(s[j]-'0');
}
ans=max(ans,a+b-c*d/e);
if(n<6)break;
a=0;b=0;
for(int j=1;j<=n-3-i;j++)
{
b=b*10+(s[j]-'0');
}
a=s[0]-'0';
ans=max(ans,(a+b)-(c*d/e));
a=0;b=0;c=0;d=0;e=0;
}
printf("Case #%d: %lld\n",k,ans);
}
return 0;
}