hdu 5938 Four Operations(模拟)
来源:互联网 发布:九堡做淘宝的在哪 编辑:程序博客网 时间:2024/06/05 17:03
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
题目大意:
给出一个长度大于5的只由‘1’~‘9’组成的字符串,现在给字符串之间找出4个空格,按照先后顺序填下‘+’、‘-’、‘*’、‘/(整除)’四个符号,要求最后计算的结果最大。
解题思路:
首先应该用‘-’分成两部分:
第一部分:左边的两个加数和尽量大;
因为和尽量大,那么可以肯定的是有一个加数一定只有一位数,因此可以从取第一位作为加数或者取‘-’前面一位作为加数的两种情况考虑,比较两种结果孰大孰小即可。
第二部分:右边的两个数相乘再整除后面一个数的商尽量小;
易得,两个数相乘一定是两个一位数,剩下的一定是作为除数!
另外,一定要注意的是,结果初始值一定要足够小!
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define INF 10000000000000005char s[105];long long check(int l,int r){ long long sum=0; while(l<=r) sum=sum*10+s[l++]-'0'; return sum;}int main(){ long long t,tt=1; cin>>t; while(t--) { cin>>s; int len=strlen(s)-1; long long a,b,c,d,e,ans=-18888,maxadd=-1,minmul=INF; //ans尽量小 for(int i=1;i<len-2;i++) { long long x=s[0]-'0',y=s[i]-'0'; long long aa=check(1,i); aa+=x; long long bb=check(0,i-1); bb+=y; if(aa<bb) //比较两种情况的结果大小 aa=bb; maxadd=aa; c=s[i+1]-'0'; d=s[i+2]-'0'; //取两个乘数 作为被除数 e=check(i+3,len); //剩下的就是除数了 minmul=c*d/e; if(ans<maxadd-minmul) //依次枚举,取最大值 ans=maxadd-minmul; } printf("Case #%lld: %lld\n",tt++,ans); } return 0;}
- 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(DP)
- 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年中国大学生程序设计竞赛(杭州))
- C#中集合里面放集合的细节问题
- Linux环境下nodejs的安装图文教程
- Find the Difference
- 算法设计与分析HW3:LeetCode3
- startService启动过程分析
- hdu 5938 Four Operations(模拟)
- Python中的None与 NULL(即空字符)的区别
- [译]Android注解--@hide
- 异常:java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 解决办法
- c++第二次作业-百钱百鸡问题
- 开发环境搭建---交叉工具链、makefile编写、连接器脚本、Eclipse调试
- Java如何生成Code128C条形码
- hdu 1848 Fibonacci again and again (博弈)
- 蓝桥杯第六届 格子中输出(printf()的*修饰符)