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

来源:互联网 发布:权力的游戏小玫瑰知乎 编辑:程序博客网 时间:2024/05/16 05:29

Four Operations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22    Accepted Submission(s): 12


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
1T105
5length of string20
 

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
112345
 

Sample Output
Case #1: 1
 

Source
2016年中国大学生程序设计竞赛(杭州)
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5943 5942 5941 5940 5939 
 

Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5938

题目大意:

  给一串长度为5~20的数字串(只含1~9),要求按顺序将 + - * / 各1个分别插入数字之间,使得最终运算结果最大。

题目思路:

  【贪心】

  分析*,因为前面是-号所以希望乘的数小,所以乘和- /之间只隔1位。

  分析/,除只可能除1位数或2位数(因为乘只乘1位,3位不优,2位可能是111991,原先我以为只要/一位)。

  分析+,可以知道位数越大加的结果越大,所以+只能是1位数与多位数相加。

  所以综上可以分类讨论,/1位或2位,乘紧挨/前,-紧挨*,之前的数字要么在第一个断开,要么最后一个。

  答案取最大即可。



////by coolxxx//#include<bits/stdc++.h>#include<iostream>#include<algorithm>#include<string>#include<iomanip>#include<map>#include<stack>#include<queue>#include<set>#include<bitset>#include<memory.h>#include<time.h>#include<stdio.h>#include<stdlib.h>#include<string.h>//#include<stdbool.h>#include<math.h>#pragma comment(linker,"/STACK:1024000000,1024000000")#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))#define abs(a) ((a)>0?(a):(-(a)))#define lowbit(a) (a&(-a))#define sqr(a) ((a)*(a))#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))#define mem(a,b) memset(a,b,sizeof(a))#define eps (1e-8)#define J 10000#define mod 1000000007#define MAX 0x7f7f7f7f#define PI 3.14159265358979323#define N 24using namespace std;typedef long long LL;typedef unsigned long long ULL;double anss;LL aans;int cas,cass;int n,m,lll,ans;LL sum,tot,s1,s2;char s[N];int main(){#ifndef ONLINE_JUDGEW//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);#endifint i,j,k;int x,y,z;//init();//for(scanf("%d",&cass);cass;cass--)for(scanf("%d",&cas),cass=1;cass<=cas;cass++)//while(~scanf("%s",s))//while(~scanf("%d%d",&n,&m)){tot=0;sum=0;s1=0;s2=0;printf("Case #%d: ",cass);scanf("%s",s);n=strlen(s);tot=-(s[n-3]-'0')*(s[n-2]-'0')/(s[n-1]-'0');s1=s[0]-'0';s2=0;for(i=1;i<n-3;i++)s2=s2*10+s[i]-'0';sum=tot+s1+s2;s1=s[n-4]-'0';s2=0;for(i=0;i<n-4;i++)s2=s2*10+s[i]-'0';tot+=s1+s2;sum=max(sum,tot);if(n>5){tot=-(s[n-4]-'0')*(s[n-3]-'0')/((s[n-2]-'0')*10+s[n-1]-'0');s1=s[0]-'0';s2=0;for(i=1;i<n-4;i++)s2=s2*10+s[i]-'0';tot+=s1+s2;sum=max(sum,tot);tot=-(s[n-4]-'0')*(s[n-3]-'0')/((s[n-2]-'0')*10+s[n-1]-'0');s1=s[n-5]-'0';s2=0;for(i=0;i<n-5;i++)s2=s2*10+s[i]-'0';tot+=s1+s2;sum=max(sum,tot);}printf("%lld\n",sum);}return 0;}/*////*/


0 0
原创粉丝点击