题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5938
Four Operations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1200 Accepted Submission(s): 352
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
Source
2016年中国大学生程序设计竞赛(杭州)
Recommend
liuyiding
Statistic |
Submit |
Discuss |
Note
题意:一个字符串,加上 + - * / 求最大数
解析:只有a+b-c*d/e得到的值才是最大值,那咱直接模拟减号的位置就好了,使a+b尽可能大,c*d/e尽可能小,那么c,d只有一位,a + b = max(a一位, b一位);
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#define N 109
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
long long num[N][N];
char s[N];
void init()
{
for(int i = 1; s[i]; i++)
{
num[i][i - 1] = 0ll;
for(int j = i; s[j]; j++)
num[i][j] = num[i][j - 1] * 10ll + s[j] - '0';
}
}
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
scanf("%s", &s[1]);
int len = strlen(&s[1]);
init();
long long ans = -79;
for(int i = 2; i <= len - 3; i++)
ans = max(ans, max(num[1][1] + num[2][i], num[1][i - 1] + num[i][i]) - num[i + 1][i + 1] * num[i + 2][i + 2] / num[i + 3][len]);
printf("Case #%d: %lld\n", ++cnt, ans);
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-670122.html