heu 5938

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

Four Operations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3285    Accepted Submission(s): 936


 

Problem Description

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 5 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.

 

 

Input

First line contains an integer T, which indicates the number of test cases.

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

Limits
1≤T≤105
5≤length of string≤20

 

 

Output

For 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

 

 

Source

2016年中国大学生程序设计竞赛(杭州)

 

注意结果可能是负数

 

#include <bits/stdc++.h>
using namespace std;
 #define ll long long
int main()
{
      int t;
      scanf("%d",&t );
      for(int tt=1;tt<=t;tt++) {
            char a[30];
            scanf("%s",a);
            ll ans=-10347812;
            int len=strlen(a);
            ll w=(ll)a[len-3]-'0';
            ll x=(ll)a[len-2]-'0';
            ll z=(ll)a[len-1]-'0';
            z=w*x/z;
            ll u,v;
            u=0,v=0;
            for(int i=0;i<len-4;i++)
            {
                  u=u*10+a[i]-'0';
                  v=0;
                  for(int j=i+1;j<len-3;j++)
                  {
                        v=v*10+a[j]-'0';
                  }
                  ans=max(ans,u+v-z);
                  //printf("%lld+%lld-%lld=%lld\n",u,v,z,u+v-z );
            }
            x=(ll)a[len-4]-'0';
            z=(ll)a[len-2]-'0';
            z=z*10+(ll)a[len-1]-'0';
            z=w*x/z;
            u=0;
            for(int i=0;i<len-5;i++)
            {
                  u=u*10+a[i]-'0';
                  v=0;
                  for(int j=i+1;j<len-4;j++)
                  {
                        v=v*10+a[j]-'0';
                  }
                  //printf("%lld+%lld-%lld=%lld\n",u,v,z,u+v-z );
                  ans=max(ans,u+v-z);
            }


            printf("Case #%d: %lld\n",tt,ans);
      }

      return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值