Four Operations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1574 Accepted Submission(s): 457
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.
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
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
Recommend
题意
给你一个字符串s,让你把它分成五个数,依次是abcde,使得a+b-c*d/e的值最大
思路
字符串长度应该为len
len=len1+1+1+len2;
应该使a+b大,a=s(0,len1-1)+b=s[len1]或a=s[0]与b=s(1,len1)比较两个谁大;
c=s[len1+1]
d=s[len1+2];
e=s(len1+3,len-1);
这暴力枚举len1的长度,或者枚举len2的长度
求出最大
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int t,T=1;
char s[100];
long long len,ans,maxx;
long long a,b,c,d,e;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
len=strlen(s);
maxx=-1000000000000000000;
for(int i=1;i<=len-4;i++)
{
e=0;
for(int j=len-i;j<=len-1;j++)
e=e*10+(s[j]-'0');
d=s[len-i-1]-'0';
c=s[len-i-2]-'0';
b=0;
for(int j=1;j<=len-i-3;j++)
b=b*10+(s[j]-'0');
a=s[0]-'0';
ans=a+b;
b=s[len-i-3]-'0';
a=0;
for(int j=0;j<=len-i-4;j++)
a=a*10+(s[j]-'0');
if(ans<(b+a))
ans=a+b;
// printf("%d %d %d %d %d\n",ans,c,d,e,c*d/e);;
ans=ans-(int)(c*d/e);
if(maxx<ans)
maxx=ans;
}
printf("Case #%d: %lld\n",T++,maxx);
}
}