HDU 5938 Four Operations 【字符串处理,枚举,把数字字符串变为数值】
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
【题意】:给一个字符串,按+, -, *, /的顺序插入将字符串分成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 【字符串处理,枚举,把数字字符串变为数值】的更多相关文章
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
Four Operations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 5938 Four Operations(乱搞)题解
题意:把'+', '-', '*' 和'/'按顺序插入任意两数字间隔,使得操作得到后计算后最大. 思路:没想到是个水题,打的时候想得太复杂了.这道题其实只要考虑*和/.显然我们要把a*b/c弄到最小. ...
- QT枚举类型与字符串类型相互转换
在QT中将枚举类型注册(QT_Q_ENUM或QT_Q_FLAG)后,就可以利用QT的元对象进行枚举类型与字符串类型转换了. 代码示例: #include <QtCore/QMetaEnum> ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 枚举类型与字符串ConvertEnumToString
枚举类型与字符串添加字典互转ConvertEnumToString using UnityEngine; using System.Collections; using UnityEngine.UI; ...
- HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)
Isabella's Message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Java枚举类型的使用,数值的二进制表示
一.Java枚举类型的使用 首先请看这段代码: package java上课; public class EnumTest { public static void main(String[] arg ...
- JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)
一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...
随机推荐
- P3509 [POI2010]ZAB-Frog
题目描述 On the bed of one particularly long and straight Byteotian brook there lie rocks jutting above ...
- 2018牛客多校第三场 C.Shuffle Cards
题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Java的Properties使用及格式定义
java.util.Properties extends Hashtable<Object,Object> 方便读取 键值对 格式的文本资源工具 常用方法一览 初始化对象 new Prop ...
- CentOS 7, Attempting to create directory /root/perl5
By francis_hao Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...
- [bzoj 2115]线性基+图论
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 给定一个带权无向图,要找出从1到n路径权值异或和最大的那一条的路径异或和. 考虑1到 ...
- 用@Component注解代替@Configuration注解,定义bean
package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...
- Maven 标准目录结构
Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目,为以后的维护工作也打下良好的基础.Maven2根据业界公认的最佳目录结构,为开发者提供了缺省的标准目录模板.Maven2的标准目录结 ...
- Python之日志操作(logging)
import logging 1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...
- xiaoluo同志Linux学习之CentOS6.4
小罗同志写的不错,弄个列表过来啊 Linux学习之CentOS(三十六)--FTP服务原理及vsfptd的安装.配置 xiaoluo501395377 2013-06-09 01:04 阅读:56 ...