HDU5938 Four Operations

Four Operations

Title link
[topic description]
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4153 Accepted Submission(s): 1193

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.

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
[Question meaning]
Given a string containing only '1'-'9' and length 5-20, split the string into five numbers A, B, C, D, E, Add the symbols +, -, *, / in order between the five numbers, namely A+BC*D/E, and find the largest calculation result.
[Thinking] It’s
just a personal opinion.
In order, calculate */ first, then ±, so the smaller the C*D/E part is, the better, and the C*D must be the smaller the better, so there are two situations, C*D>E or C*D <=E, if C*D>E, then E needs to become two digits to make C*D/E the smallest, so the divisor E can only be one digit or two digits, C and D are both one digit, and the remainder The following A and B need to form as large a number as possible. Only the number composed of the first digit plus the remaining characters, or the number composed of the last digit plus the remaining characters, take the maximum value, see the figure for details
Insert picture description here
[ Code]

#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<minmax.h>
using namespace std;
int main() {
    
    
//	freopen("1.txt","r",stdin);
	int T;
	char s[30];
	scanf("%d",&T);
	for(int t=1; t<=T; t++) {
    
    
		scanf("%s",s);
		int len=strlen(s);
		//除数是一位时
		long long int sum=0;//位数较大,所以需要用long long int 
		long long int a,a1=0,a2=0,b,c,d;
		//求两种组合方式的最大值 
		for(int i=1; i<=len-4; i++) {
    
    
			a1=a1*10+int(s[i]-48);
		}
		a1+=int(s[0]-48);
		for(int i=0; i<len-4; i++) {
    
    
			a2=a2*10+int(s[i]-48);
		}
		a2+=int(s[len-4]-48);
		a=max(a1,a2);
		b=int(s[len-3]-48);
		c=int(s[len-2]-48);
		d=int(s[len-1]-48);
		sum=a-b*c/d;
		//除数是两位时
		if(len>5) {
    
    
			long long int sum2=0;
			a,a1=0,a2=0,b,c,d;
			for(int i=1; i<=len-5; i++) {
    
    
				a1=a1*10+int(s[i]-48);
			}
			a1+=int(s[0]-48);
			for(int i=0; i<len-5; i++) {
    
    
				a2=a2*10+int(s[i]-48);
			}
			a2+=int(s[len-5]-48);
			a=max(a1,a2);
			b=int(s[len-4]-48);
			c=int(s[len-3]-48);
			d=int(s[len-2]-48)*10+int(s[len-1]-48);
			sum2=a-b*c/d;
			sum=max(sum,sum2);
		}
		printf("Case #%d: %lld\n",t,sum);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/lmmmmmmmmmmmmmmm/article/details/89328445