HDU 5938 Four Operations(暴力+贪心)

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

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
long long max(long long x,long long y)
{
	return x>y?x:y;
}
long long ans=-999999;
int t,a[50],k,i;
char s[50],laji[10];
long long sum(int l,int r)
{
	long long ans1=0;
	int j;
	for(j=l;j<=r;j++)
	{
		ans1=ans1*10+a[j];
	}
	return ans1;
}
// a+b-c*d/e
//a 1 or b 1
//c,d 1
//e 1 or 2
int main()
{
	scanf("%d",&t);
	for(k=1;k<=t;k++)
	{
		scanf("%s",s);
		ans=0;
		int len=strlen(s);
		for(i=0;i<len;i++)
		{
			a[i]=s[i]-'0';
		}
		ans=a[0]+sum(1,len-4)-a[len-3]*a[len-2]/a[len-1];
		ans=max(ans,sum(0,len-5)+a[len-4]-a[len-3]*a[len-2]/a[len-1]);
		if(len>5)
		{
			ans=max(ans,a[0]+sum(1,len-5)-a[len-4]*a[len-3]/sum(len-2,len-1));		
			ans=max(ans,sum(0,len-6)+a[len-5]-a[len-4]*a[len-3]/sum(len-2,len-1));
		}
		printf("Case #%d: ",k);
		printf("%lld\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值