Four Operations HDU - 5938

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. 

Every test contains one line with a string only contains digits '1''9'

Limits 
1T1051≤T≤105 
5length of string205≤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
12345
Sample 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章