HDU 5938 Four Operations(四则运算)

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

 

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.
  1. Ruins是个好学的男孩纸,最近他在学四则运算!
  2.  
  3. 现在他打算练练四则运算。此处有个由数字'' - ''组成的字符串,依次添加'+', '-', '*' , '/'这四个运算符把字符串划分为5个部分,再算出结果(/ 使用整数除法)。
  4.  
  5. 帮他找出可以获得的最大值吧!

CN

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
  1. 第一行为一个整数T,表示测试用例的数量。
  2.  
  3. 每个测试用例为一个仅由数字 ''-'' 组成的字符串。
  4.  
  5. 数据范围
  6. <= T <= ^
  7. <= 字符串长度 <=

CN

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.
  1. 对于每组测试用例,输出"Case #x: y"x表示从1开始的用例编号,y为结果。

CN

Sample Input - 输入样例

  1. 1
  2. 12345

Sample Output - 输出样例

  1. Case #1: 1

题解

  模拟水题。
  一个只有5个部分,可以写成A+B-C*D/E
  要使结果最大,则A+B最大,C*D/E最小
  A+B最大,加号要么在第一位数后面,要么在最后一位数前面。
  C*D/E最小,C和D都是1位数,E只有可能是1~3位数,到3位数的时候已经为0了。
  所以最多只要就算三次即可,注意初始化。

代码 C++

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define ll __int64
  5. char data[];
  6.  
  7. int main(){
  8. int t, it, i, j, len;
  9. ll opt, l, r, L, R, L10, R10;
  10. for (it = scanf("%d ", &t); it <= t; ++it){
  11. opt = (ll) << ;
  12. gets(data); len = strlen(data);
  13. L = ; L10 = ;
  14. for (i = ; i < len - ; ++i, L10 *= ) L = L * + data[i] - '';
  15. L10 /= ;
  16. R = ; R10 = ;
  17. j = std::min(, len - );
  18. for (i = ; i <= j; ++i){
  19. R = (data[len - i] - '')*R10 + R;
  20. R10 *= ;
  21. l = std::max(L / L10 + L % L10, L / + L % );
  22. L10 /= ; L /= ;
  23. r = (data[len - i - ] - '')*(data[len - i - ] - '') / R;
  24. opt = std::max(opt, l - r);
  25. }
  26. printf("Case #%d: %I64d\n", it, opt);
  27. }
  28. return ;
  29. }

HDU 5938 Four Operations(四则运算)的更多相关文章

  1. HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))

    Four Operations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】

    Problem Description Little Ruins is a studious boy, recently he learned the four operations! Now he ...

  3. HDU 5938 Four Operations(乱搞)题解

    题意:把'+', '-', '*' 和'/'按顺序插入任意两数字间隔,使得操作得到后计算后最大. 思路:没想到是个水题,打的时候想得太复杂了.这道题其实只要考虑*和/.显然我们要把a*b/c弄到最小. ...

  4. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

  5. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  6. hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  7. HDU 6315: Naive Operations

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  8. HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

    hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...

  9. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

随机推荐

  1. ios枚举规范

  2. Python--循环语句

    Python 循环语句 循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式: Python提供了for循环和while循环(在Python中没有do..while循 ...

  3. mybatis比较数字或者单字母

    http://blog.csdn.net/alibert/article/details/50177017 <if test="req.queryType == '3'.toStrin ...

  4. Web服务之LNMMP架构及动静分离实现

    原文链接:http://hoolee.blog.51cto.com/7934938/1413346 讲的非常详细,尽管我只看动静分离,可是看了一下其他的部署,也是非常不错,适合新手 一.LNMMP  ...

  5. Linux系统安装-系统分区

    ctrl+alt+enter全屏

  6. [Struts2] Action Implements SessionAware

    struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...

  7. html5,格式的验证

    <form action="" method="get">    <input type="text" name=&quo ...

  8. 【原创】如何在Android Studio下调试原生安卓Framework层面的源代码

    1. Open Existing Android Studio Project. 2. 打开后, Projects -> Android 里面是空的. 这时候,需要选到 Projects-> ...

  9. 实现一个自适应网页用到的css知识

    1 div导致父级高度没有变化是应该子元素设置了float:left等 2 div元素居中 text-align:center; margin-left: auto;margin-right: 3 大 ...

  10. 从零开始调用一个手机号归属地查询API

    自从过上程序员的生活,身边总是或多或少的提及一些API(应用程序接口),网上各种入门教程.实例大把大把,有的只是贴上部分代码,也不给注释, 写Demo的时候连编译都无法通过.下面我从小白开始来介绍下调 ...