HDU-5938:Four Operations(DP)
来源:互联网 发布:化工项目网络计划绘制 编辑:程序博客网 时间:2024/05/17 09:15
Four Operations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2082 Accepted Submission(s): 608
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 into5 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.
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
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
Every test contains one line with a string only contains digits '1'-'9'.
Limits
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
112345
Sample Output
Case #1: 1
思路:d[i]表示[0,i]间放'+'的最大值,f[i]表示[i,n]间放'*'和'/'后的最小值。ans=max(d[i]-f[i+1]);
#include<bits/stdc++.h>using namespace std;char s[30];long long d[30],f[30],n;long long num(int x,int y){ long long sum=0; for(int i=x;i<=y;i++)sum=sum*10+s[i]-'0'; return sum;}int main(){ int T,cas=1;cin>>T; while(T--) { memset(d,0,sizeof d); memset(f,1e9+7,sizeof f); scanf("%s",s); n=strlen(s); for(int i=0;i<n-3;i++) { for(int j=0;j<i;j++)d[i]=max(d[i],num(0,j)+num(j+1,i)); } for(int i=n-1;i>=2;i--) { for(int j=i;j<=n-1;j++) { for(int k=j+1;k<n-1;k++)f[i]=min(f[i],num(i,j)*num(j+1,k)/num(k+1,n-1)); } } long long ans=-1e9-7; for(int i=1;i<n-3;i++)ans=max(ans,d[i]-f[i+1]); printf("Case #%d: %lld\n",cas++,ans); } return 0;}
阅读全文
1 0
- HDU-5938:Four Operations(DP)
- HDU 5938 Four Operations
- hdu 5938 Four Operations
- hdu 5938 Four Operations
- HDU-5938 Four Operations(字符串处理)
- hdu 5938 Four Operations(模拟)
- hdu 5938 Four Operations(乘法四则运算)
- HDU 5938 Four Operations(枚举)
- hdu 5938 Four Operations【贪心】
- hdu 5938 Four Operations 模拟
- HDU 5938 Four Operations 贪心
- HDU 5938 Four Operations 模拟
- HDU 5938 Four Operations(思维)
- hdu 5938 Four Operations(复杂的模拟题)
- HDU 5938 Four Operations 想法题
- HDU 5938 Four Operations (水题乱搞)
- Four Operations HDU
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU
- POJ 1679 The Unique MST 次小生成树
- 一次请求的源码分析过程
- bootstrap table使用总结
- cordova构建app偶遇mergeDebugResources失败的问题
- HDU-5938:Four Operations(DP)
- 【c++基础】笔记(二)
- 面试记录第十九节——(MVC)
- Java水仙花数
- 使用Git管理代码
- java输出1~100之间的全部素数
- 【c++基础】笔记(三)
- weiwei爬虫4.4--超时设置
- 【c++基础】笔记(四)