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


12345

Sample Output

Case #1: 1

【题意】:给一个字符串,按+, -, *, /的顺序插入将字符串分成a+b-c*d/e,要求结果最大。
【分析】:枚举负号的位置,因为要使整个值最大,C*D应该最小,所以C和D都只取一位。
A+B的值最大需要使A或B的位数尽可能大,即A一位,B为到负号前的所有位或者B为符号前一位,
A为开头到负号前一位位置,两种情况取最大值。由于C和D都确定了E也随之确定了。
所以总的来说就是枚举一下负号的位置,然后判断一下A+B的最大值就可以了。 
【代码】:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <string> const int maxn=;
const int inf=0x3f3f3f3f;
typedef long long ll;
using namespace std; char a[maxn];
int num[maxn]; ll f(int s,int e)
{
ll res=;
for(int i=s; i<=e; i++)
{
res = res* + num[i] ;
}
return res;
}//
int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
scanf("%s",a);
int len=strlen(a);
ll ans = -inf; for(int i=;i<len;i++)
{
num[i+]=a[i]-'';
} for(int i=;i<=len-;i++)
{
ll add,c,d,e; add=max((f(,i-)+f(i,i)) , (f(,)+f(,i)));
c=num[i+];
d=num[i+];
e=f(i+,len); ans=max(ans,add-c*d/e);
}
printf("Case #%d: %lld\n",cas,ans);
}
return ;
}

HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】的更多相关文章

  1. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

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

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

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

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

  4. QT枚举类型与字符串类型相互转换

    在QT中将枚举类型注册(QT_Q_ENUM或QT_Q_FLAG)后,就可以利用QT的元对象进行枚举类型与字符串类型转换了. 代码示例: #include <QtCore/QMetaEnum> ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. 枚举类型与字符串ConvertEnumToString

    枚举类型与字符串添加字典互转ConvertEnumToString using UnityEngine; using System.Collections; using UnityEngine.UI; ...

  7. HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)

    Isabella's Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Java枚举类型的使用,数值的二进制表示

    一.Java枚举类型的使用 首先请看这段代码: package java上课; public class EnumTest { public static void main(String[] arg ...

  9. JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)

    一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...

随机推荐

  1. [洛谷P3413]SAC#1 - 萌数

    题目大意:求$[l,r](0\leqslant l<r< 10^{1001})$中存在长度至少为$2$的回文串的数字数 题解:数位$DP$,发现如果有回文串,若长度为偶数,一定有两个相同的 ...

  2. 【学习笔记】Learning OpenCV3——Ch8 working with video

    Reading Video with the cv::VideoCapture Object 对象创建的三种方法: // 1. Input filename cv::VideoCapture::Vid ...

  3. I/O多路转接-epoll

    By francis_hao    Aug 5,2017   APUE讲多路转接的章节介绍了select.pselect和poll函数.而epoll是linux内核在2.5.44引入的.在glibc ...

  4. linux 条件判断式

    1.利用if ...then if [ 判断条件 ];then 指令 fi 实例一 Y/N: #!/bin/bash #Program: # This program shows "Hell ...

  5. sqrti128

    求平方根下取整,对于gcc type __uint128_t. ~45.5ns/op on i7-7700k@4.35G,即typical <200cyc/op. Together with u ...

  6. SLF4J 与Log4J

    为什么要使用SLF4J而不是Log4J 每一个Java程序员都知道日志对于任何一个Java应用程序,尤其是服务端程序是至关重要的,而很多程序员也已经熟悉各种不同的日志库如java.util.loggi ...

  7. notepad++中快速插入当前时间方法

    转载自:http://blog.csdn.net/donghustone/article/details/7436483 在notepad++中快速插入当前时间方法: 插件是notepad++的一大优 ...

  8. 学习正则表达式及c#应用

    1.0正则表达式语法   正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”).模式描述在搜索文本时要匹配的一个或多个字符串. 正则表达式示例   表达式 ...

  9. 在Ubuntu下安装IntelliJ IDEA

    1)在IDEA官网下载Linux版本安装包 2)将安装包shell到/usr/local目录下 3)切到安装目录下,验证文件校验和,官网上显示的校验和: 3d77ee82094dab51e345f16 ...

  10. 51nod 1254 最大子段和 V2 ——单调栈

    N个整数组成的序列a[1],a[2],a[3],…,a[n],你可以对数组中的一对元素进行交换,并且交换后求a[1]至a[n]的最大子段和,所能得到的结果是所有交换中最大的.当所给的整数均为负数时和为 ...