2016CCPC杭州站 F - Four Operations 【枚举】

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.
Input
First 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
1≤T≤1051≤T≤105
5≤length of string≤205≤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

题意:给一个20位之内的数字,让你用分隔符分成五段。然后从左向右依次用+-*/进行运算(有优先级)。

我们不难分析出来,根据优先级减号后面的数字越小越好。这就意味着*号左右只能由两个个位数组成。

a+b-c*d/e

c和d为个位数。
a与b中有一个个位数。
那么枚举除号的位置就好了~

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
#define LL long long
int T;
char s[25];
int main()
{
    scanf("%d",&T);
    LL cas = 0;
    while(T--){
        scanf("%s",s);
        LL len = strlen(s);
        LL mx = -1e16;
        LL p[25]={1};
        for(int i=1;i<=16;i++) p[i] = p[i-1]*10;
        for(int i=3;i<len-1;i++){
            LL ans ;
            LL c = s[i-1] -'0';
            LL d = s[i] - '0';
            LL a1=0,b1=0,e=0,a2 = 0,b2=0;
            for(int j=len-1;j>i;j--){
                e+=(s[j]-'0')*p[len-1-j];
            }
            a1 = s[0] - '0';
            b2 = s[i-2] -'0';
            for(int j=i-2;j>=1;j--){
                b1 += (s[j]-'0')*p[i-2-j];
            }
            for(int j=i-3;j>=0;j--){
                a2 += (s[j]-'0')*p[i-3-j];
            }
            ans = max(a1+b1-c*d/e,a2+b2-c*d/e);
            mx = max(mx,ans);
        }
        printf("Case #%lld: %lld\n",++cas,mx);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值