2020 BIT冬训-模拟与暴力 I - Four Operations HDU - 5938
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',
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 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
要求最大值。即+-*/中-号后面最小。即a*b/c最小。
a,b越小越好。也就是一位。
c的话可能需要一位或者二位例如2312994。就需要除号后面有2位。
+号部分取第一位+到减号前数字。或前面的数字+减号前一位
因此只要枚举/后面的数的位数即可(只要枚举1,2即可。代码里面还枚举了3.(反正不影响结果))
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int t,id,flag; long long ans,c,temp; char num[25]; int main(){ scanf("%d",&t); while(t--){ scanf("%s",num); ans= -0x3f3f3f; for(int j=1;j<=3;j++){//枚举/后面有1~3位 if(strlen(num)==5&&j==2) break; else if(strlen(num)==6&&j==3) break; temp=0,c=0,flag =1; for(int i=0;i<strlen(num)-j-3;i++) if(num[i]>num[i+1]){ flag =1;//2177+5-*/而不是2+177-*/ break; }else if(num[i]<num[i+1]){ flag =0;//1+2775-*/而不是1277+5-*/ break; } if(flag){ for(int i=0;i<strlen(num)-j-3;i++){ temp*=10; temp+=num[i]-'0'; } temp+=num[strlen(num)-j-3]-'0'; }else{ for(int i=1;i<strlen(num)-j-2;i++){ temp*=10; temp+=num[i]-'0'; } temp+=num[0]-'0'; } for(int k=1;k<=j;k++){ c*=10; c += num[strlen(num)-j-1+k]-'0'; } temp = temp-( ( (num[strlen(num)-j-2]-'0')*(num[strlen(num)-j-1]-'0') ) / c ); ans =max(temp,ans); } printf("Case #%d: %lld\n",++id,ans); } }

浙公网安备 33010602011771号