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. iOS计算完整文字高度(适应iOS 10)

    动态计算文字的高度:(切记LineSapcing>=2,不然会显示不全) +(CGSize) boundingALLRectWithSize:(NSString*) txt Font:(UIFo ...

  2. Ubuntu Dev Box Setup

    Editor VIM Sublime Atom Visual Studio Code SSH Client PAC Manager File Manager Double Commander Imag ...

  3. postgresql修炼之道学习笔记(2)

    随后的章节  介绍了基础的sql,这个我略过了,我喜欢在开发的时候,慢慢的研究,毕竟有oracle的基础. 现在,学习psql工具 使用create database创建数据库的时候,出现如下问题: ...

  4. Git 取消跟踪已版本控制的文件

    http://www.cnblogs.com/cposture/p/git.html 转: Git 是一个很好的版本控制工具,当然驾驭起来相比 SVN 要稍微复杂一些.初入 Git,难免有一些问题.比 ...

  5. CentOS7下Apache及Tomcat开启SSL

    安装: 复制代码 yum install -y openssl #使用openssl可手动创建证书 yum install -y httpd yum install -y mod_ssl 防火墙打开8 ...

  6. MongoDB win安装后无法远程连接访问

    mongoDB安装后无法远程连接访问,原因是端口没有开放允许连接的权限 开启允许连接的权限: 管理工具-高级win防火墙

  7. ldap配置记录

    记录一下最近研究ldap+nfs的情况 ldap这个东西上次研究nis的时候就有人说那是上个世纪的老东西了,不应该继续使用.虽然意识到如此但时间不够还是使用了nis,这次乘着重做就干脆切换到ldap, ...

  8. Install Jenkins Slave as Windows Service

    https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...

  9. js通过隐藏iframe修改session值

    js:function selects(id, ss) { window.frames["UpSession"].window.location.href = "../U ...

  10. 自动刷新页面为了session不过期

    为了保证在打开页面期间session不过期,估做了一个隐藏Iframe每隔若干秒来刷新一下页面,在隐藏页面给session赋值. <script type="text/javascri ...