hdu 5938 Four Operations【贪心】
来源:互联网 发布:java中super关键字 编辑:程序博客网 时间:2024/05/16 12:56
Four Operations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
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 into5 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.
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
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
Every test contains one line with a string only contains digits '1'-'9'.
Limits
Output
For every test case, you should output 'Case #x: y', wherex indicates the case number and counts from 1 andy is the result.
Sample Input
1
12345
Sample Output
Case #1: 1
题目大意:
给你一个由1-9数字组成的数字串,让你将其分成五个数(不能打乱顺序)(不能为空),然后使得其五个数之间用+-*/四个符号链接起来,使得运算结果最大。
样例分析:
1+2-3*4/5=1
思路:
1、首先我们设定这五个数为a,b,c,d,e,那么其结果为:a+b-c*d/e,我们分析发现,其拆分出来的数c,d,e都是正数,那么-c*d/e一定是一个负数,那么我们第一个任务就是将其值尽可能的缩小,并且让a+b尽可能的大。
2、我们首先来分析如何让a+b尽可能的大,现在我们将问题简化,给你一个字符串,让你将其分成两个数之后求和,使其最大,那么我们一定想要将一个数设定为当前字符串长度-1的一个长度的数,另一个数设定为一个长度为1的数,那么通过这样简单分析可知,我们要么把第一个数字设定为长度为1的数,要么把第最后个数字设定为长度为1的数,那么对应两种情况,我们取最大值即可。
3、我们再来分析如何将-c*d/e这个负数变得尽量小我们将c和d都设定为长度为1的数,那么使得其乘积尽可能的小。然后将其这两个数后边的数都设定成e,(比如123456,我们可以设定3 4是c和d,那么e就是56,这样就能尽量让这个负数尽可能的小)。
对于这个e的长度,我们可以通过枚举来搞定(暴力题暴力做)。
那么我们直接枚举一个位子i,来界限a+b和-c*d/e,位子i之前的数字维护一个a+b最大,位子i后边按照上述过程计算出来,然后将两个数相减,得到一个可行解,然后在枚举位子i的过程中,维护一个最大值即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64char a[200];int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { scanf("%s",a); int n=strlen(a); ll output=-1000000000000000000; for(int i=2;i<n-2;i++) { ll aa=0; for(int j=0;j<i-1;j++) { aa=aa*10+a[j]-'0'; } ll b=a[i-1]-'0'; ll tmp=aa+b; aa=0;b=0; for(int j=1;j<i;j++) { aa=aa*10+a[j]-'0'; } b=a[0]-'0'; ll tmp2=aa+b; ll c=a[i]-'0'; ll d=a[i+1]-'0'; ll e=0; for(int j=i+2;j<n;j++) { e=e*10+a[j]-'0'; } output=max(output,max(tmp,tmp2)-c*d/e); } printf("Case #%d: ",++kase); printf("%I64d\n",output); }}
0 0
- hdu 5938 Four Operations【贪心】
- HDU 5938 Four Operations 贪心
- HDU 5938 Four Operations
- hdu 5938 Four Operations
- hdu 5938 Four Operations
- hdu 5938 Four Operations 模拟
- HDU 5938 Four Operations 模拟
- HDU 5938 Four Operations(思维)
- hdu5938 Four Operations 贪心
- hdu5938 Four Operations(贪心)
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
- HDU-5938 Four Operations(字符串处理)
- HDU 5938 Four Operations 想法题
- HDU 5938 Four Operations (水题乱搞)
- hdu 5938 Four Operations(模拟)
- hdu 5938 Four Operations(乘法四则运算)
- HDU 5938 Four Operations(枚举)
- HDU-5938:Four Operations(DP)
- 山东省第一届ACM大学生程序设计竞赛 problem D Greatest Number
- 基于3D卷积神经网络的人体行为理解(论文笔记)
- 【传感器】BMA253 数字,三轴加速度传感器
- css中背景图片的显示位置
- Windbg 内核调试 Dump文件分析
- hdu 5938 Four Operations【贪心】
- 主流浏览器内核及JS引擎
- WeakHashMap(二)
- Codeforces 94A-Restoring Password
- Hibernate与 MyBatis对比
- SSD: Single Shot MultiBox Detector 检测单张图片
- Hive 运行三种方式之 本地mysql
- markdown插入excel表格
- C++ vector的说明与应用下