Four Operations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1164 Accepted Submission(s): 341
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
一年前用暴力过了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;
typedef long long LL;
const int N = 30;
LL n, k, ans;
char str[N];
LL maxt(LL x,LL y)
{
return x>y?x:y;
}
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%s",str+1);
ans=-9999999999;
int len=strlen(str+1);
LL s1=0,s2,s3,s4,s5;
for(int i=1;i<=len-4;i++)
{
s1=s1*10+(str[i]-'0');
s2=0,s3=0,s4=0,s5=0;
for(int j=i+1;j<=len-3;j++)
{
s2=s2*10+(str[j]-'0');
s3=s4=s5=0;
for(int k=j+1;k<=len-2;k++)
{
s3=s3*10+(str[k]-'0');
s4=s5=0;
for(int p=k+1;p<=len-1;p++)
{
s4=s4*10+(str[p]-'0');
s5=0;
for(int q=p+1;q<=len;q++)
{
s5=s5*10+(str[q]-'0');
}
ans=maxt(ans,s1+s2-s3*s4/s5);
}
}
}
}
printf("Case #%d: %I64d\n",ncase++,ans);
}
return 0;
}
一年后学了STL和库函数 T了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
const LL mod = 1e9+7;
int a[N];
string str;
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
cin>>str;
int len=str.length();
LL ans=0;
for(int i=0;i<=len-5;i++)
{
for(int j=i+1;j<=len-4;j++)
{
for(int k=j+1;k<=len-3;k++)
{
for(int h=k+1;h<=len-2;h++)
{
string s1=str.substr(0,i+1);
LL a1=atoi(s1.c_str());
string s2=str.substr(i+1,j-i);
LL a2=atoi(s2.c_str());
string s3=str.substr(j+1,k-j);
LL a3=atoi(s3.c_str());
string s4=str.substr(k+1,h-k);
LL a4=atoi(s4.c_str());
string s5=str.substr(h+1,len-1-h);
LL a5=atoi(s5.c_str());
ans=max(ans,a1+a2-a3*a4/a5);
}
}
}
}
printf("Case #%d: %I64d\n",ncase++,ans);
}
return 0;
}
学了泰神的思维,将任意两个区间的数的值预处理出来减少了循环的次数,然后分析问题因为要取最小值,所以a+b尽量大 (b*c/d)尽量小
要使a+b尽量大,要使位数尽量长必定是一位数+其余位数,(b*c)尽量小,d尽量大,肯定是一位数乘以一位数除以剩余的数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
const LL mod = 1e9+7;
char str[N];
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
scanf("%s",str+1);
int len=strlen(str+1);
LL num[25][25];
memset(num,0,sizeof(num));
for(int i=1;i<=len;i++)
{
num[i][i-1]=0;
for(int j=i;j<=len;j++)
{
num[i][j]=num[i][j-1]*10+str[j]-'0';
}
}
LL ans=-99999999;
for(int i=2;i<=len-3;i++)
{
ans=max(ans,max(num[1][1]+num[2][i],num[1][i-1]+num[i][i])-num[i+1][i+1]*num[i+2][i+2]/num[i+3][len]);
}
printf("Case #%d: %I64d\n",ncase++,ans);
}
return 0;
}