HDU-5938 Four Operations 字符串处理 ʕ •ᴥ•ʔ

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

题意:给你一串数字 让你分成5个部分  依次进行加减乘除 运算 求结果得最大值  可以分析出 要想做大 得a+b最大 c*d/e最小

那么c d 只能为一位数 两个一位数相乘 只能得到一个二位数 所以e 只能为一位或两位数 为什么是两位数 看例子 (111991) 然后就判断 a,b怎么组合最大

#include
#include
#include
#include
#include
#include
#include
#define N 0x3f3f3f3f
#define ll long long
using namespace std;
int num[30];
ll f(int a,int b)
{
	ll ans=0;
	for(int i=a;i<=b;i++)
	{
		ans=ans*10;
		ans+=num[i];
	}
	return ans;
}
int main()
{
	int t;
	cin>>t;
	int Case=1;
	while(t--)
	{
		string s;
		cin>>s;
		int len=s.size();
		for(int i=0;i5)
		{	
			int dis=f(len-2,len-1);
			sum=max(sum,num[0]+f(1,len-5)-num[len-4]*num[len-3]/dis);
			sum=max(sum,f(0,len-6)+num[len-5]-num[len-4]*num[len-3]/dis);
		}
		printf("Case #%d: %lld\n",Case++,sum);
	}
	return 0;
} 

 

你可能感兴趣的:(HDU-5938 Four Operations 字符串处理 ʕ •ᴥ•ʔ)