2016CCPC杭州站 F

来源:互联网 发布:微电脑治疗仪淘宝网 编辑:程序博客网 时间:2024/06/10 23:57

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