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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值