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.
小Ruins是个好学的男孩纸,最近他在学四则运算!

现在他打算练练四则运算。此处有个由数字'' - ''组成的字符串,依次添加'+', '-', '*' , '/'这四个运算符把字符串划分为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
第一行为一个整数T,表示测试用例的数量。

每个测试用例为一个仅由数字 ''-'' 组成的字符串。

数据范围
<= T <= ^
<= 字符串长度 <=

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

CN

Sample Input - 输入样例

1
12345

Sample Output - 输出样例

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++

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define ll __int64
char data[]; int main(){
int t, it, i, j, len;
ll opt, l, r, L, R, L10, R10;
for (it = scanf("%d ", &t); it <= t; ++it){
opt = (ll) << ;
gets(data); len = strlen(data);
L = ; L10 = ;
for (i = ; i < len - ; ++i, L10 *= ) L = L * + data[i] - '';
L10 /= ;
R = ; R10 = ;
j = std::min(, len - );
for (i = ; i <= j; ++i){
R = (data[len - i] - '')*R10 + R;
R10 *= ;
l = std::max(L / L10 + L % L10, L / + L % );
L10 /= ; L /= ;
r = (data[len - i - ] - '')*(data[len - i - ] - '') / R;
opt = std::max(opt, l - r);
}
printf("Case #%d: %I64d\n", it, opt);
}
return ;
}

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. Java实现冒泡排序

    冒泡排序思想就是将数列的相邻两个数比较,较大的数往后保存,小的数往前. package Sort; import java.util.Arrays; public class BubbleSort { ...

  2. Python模块和包

    模块和包是python组织代码的基本方式. 模块: python的每一个脚本文件都可称之为模块,模块的名称就是脚本的文件名.例如当我们写了一个test.py的脚本文件,则可以在同目录下的另外一个脚本m ...

  3. div中iframe高度自适应问题

    网页分为上.中.下三部分,上.下高度固定中间高度自适应:中间分为左.右两部分,左边宽度固定,右边宽度自适应.现在右侧div是宽度和高度都是自适应,右侧div里有个IFrame,想让IFrame自适应外 ...

  4. 编译错误:/usr/bin/ld: cannot find -lz

    编译时出现错误/usr/bin/ld: cannot find -lz,安装zlib和zlib-devel yum install zlib yum install zlib-devel

  5. C++之路进阶——P2022

    P2022 有趣的数 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的位置为Q( ...

  6. 【皇甫】☀IOC和AOP的拓展实例

    <!--构造器注入 --> <bean id="user1" class="cn.happy.entity.User"> <con ...

  7. Ext中renderer用法及参数

    转载处:http://blog.csdn.net/yangxiaojun9238/article/details/8240139 setHeader是设置http恳求的头是MIME 和谈的拓展可以实现 ...

  8. iOS传值之block传值(一)

    ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...

  9. java文件编程总结

    文件编程之一就是文件操作,就是新建,删除,复制,更名 新建文件用createNewFile()方法,要处理IOException异常 新建文件夹用mkdirs()方法 删除一个文件和一个空的文件夹直接 ...

  10. YbSoftwareFactory 代码生成插件【十九】:实体类配合数据库表字段进行属性扩展的小技巧

    实体类通常需要和数据库表进行了ORM映射,当你需要添加新的属性时,往往同时也需要在数据库中添加相应的字段并配置好映射关系,同时可能还需对数据访问组件进行重新编译和部署才能有效.而当你开始设计一个通用数 ...