您的位置:首页 > 运维架构

HDU 5938 - Four Operation(贪心)

2017-01-21 21:58 281 查看

Four Operations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 754    Accepted Submission(s): 269


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

1
12345

 

[align=left]Sample Output[/align]

Case #1: 1

 

[align=left]Source[/align]
2016年中国大学生程序设计竞赛(杭州)

 

[align=left]Recommend[/align]
liuyiding   |   We have carefully selected several similar problems for you:  6010 6009 6008 6007 6006

/*
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938

 题目大意:
给你一个由1-9数字组成的数字串,让你将其分成五个数(不能打乱顺序)(不能为空),
然后使得其五个数之间用+ - * /四个符号链接起来,使得运算结果最大。
例如输入数字串为 12345 则最大为 1+2-3*4/5=1

思路:
1、首先我们设定这五个数为a,b,c,d,e,那么其结果为:a+b-c*d/e,
我们分析发现,其拆分出来的数c,d,e都是正数,那么-c*d/e一定是一个负数,
那么我们第一个任务就是将其值尽可能的缩小,并且让a+b尽可能的大。
2、我们首先来分析如何让a+b尽可能的大,现在我们将问题简化,给你一个字符串,
让你将其分成两个数之后求和,使其最大,
那么我们一定想要将一个数设定为当前字符串长度-1的一个长度的数,
另一个数设定为一个长度为1的数,那么通过这样简单分析可知,
我们要么把第一个数字设定为长度为1的数,要么把第最后个数字设定为长度为1的数,
那么对应两种情况,我们取最大值即可。
3、我们再来分析如何将-c*d/e这个负数变得尽量小我们将c和d都设定为长度为1的数,
那么使得其乘积尽可能的小。然后将其这两个数后边的数都设定成e,
(比如123456,我们可以设定3 4是c和d,那么e就是56,这样就能尽量让这个负数尽可能的小)。
对于这个e的长度,我们可以通过枚举来搞定(暴力题暴力做)。

摘自: http://blog.csdn.net/mengxiang000000/article/details/52965276
直接枚举a和b这两个数字总共所占用的字符数
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;

string s;
int len;
LL ans;
int main(
4000
void)
{
// freopen("in.txt","r", stdin);
int T, cas = 1;
cin >> T;
while (T--)
{
cin >> s;
len = s.length();
ans = -INF;
LL a, b, c, d, e;
LL abmax;
for (int i = 1; i < len-3; i++){
a = s[0] - '0';
b = 0;
for (int j = 1; j <= i; j++)
b = b * 10 + s[j]-'0';
abmax = a + b;
a = 0;
b = s[i] - '0';
for (int j = 0; j < i; j++)
a = a * 10 + s[j]-'0';
abmax = max(abmax, a + b);

c = s[i+1] - '0';
d = s[i+2] - '0';
e = 0;
for (int j = i+3; j < len; j++)
e = e * 10 + s[j]-'0';
ans = max(ans, abmax-c*d/e);

}

printf("Case #%d: %I64d\n", cas++, ans);
}

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: