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

本文探讨了一道经典的算法问题,即如何通过在数字串中插入四则运算符(+、-、*、/)来获得最大的计算结果。文章详细解析了贪心策略的应用,通过分类讨论除法操作的位数,以及乘法、加法和减法的最优位置,从而找到最优化解。

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
1 12345
 

 

Sample Output
Case #1: 1
 

 

Source
 

 

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位,乘紧挨/前,-紧挨*,之前的数字要么在第一个断开,要么最后一个。

  答案取最大即可。

 

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<queue>
11 #include<set>
12 #include<bitset>
13 #include<memory.h>
14 #include<time.h>
15 #include<stdio.h>
16 #include<stdlib.h>
17 #include<string.h>
18 //#include<stdbool.h>
19 #include<math.h>
20 #pragma comment(linker,"/STACK:1024000000,1024000000")
21 #define min(a,b) ((a)<(b)?(a):(b))
22 #define max(a,b) ((a)>(b)?(a):(b))
23 #define abs(a) ((a)>0?(a):(-(a)))
24 #define lowbit(a) (a&(-a))
25 #define sqr(a) ((a)*(a))
26 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
27 #define mem(a,b) memset(a,b,sizeof(a))
28 #define eps (1e-8)
29 #define J 10000
30 #define mod 1000000007
31 #define MAX 0x7f7f7f7f
32 #define PI 3.14159265358979323
33 #define N 24
34 using namespace std;
35 typedef long long LL;
36 typedef unsigned long long ULL;
37 double anss;
38 LL aans;
39 int cas,cass;
40 int n,m,lll,ans;
41 LL sum,tot,s1,s2;
42 char s[N];
43 int main()
44 {
45     #ifndef ONLINE_JUDGEW
46 //    freopen("1.txt","r",stdin);
47 //    freopen("2.txt","w",stdout);
48     #endif
49     int i,j,k;
50     int x,y,z;
51 //    init();
52 //    for(scanf("%d",&cass);cass;cass--)
53     for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
54 //    while(~scanf("%s",s))
55 //    while(~scanf("%d%d",&n,&m))
56     {
57         tot=0;sum=0;s1=0;s2=0;
58         printf("Case #%d: ",cass);
59         scanf("%s",s);
60         n=strlen(s);
61         tot=-(s[n-3]-'0')*(s[n-2]-'0')/(s[n-1]-'0');
62         s1=s[0]-'0';s2=0;
63         for(i=1;i<n-3;i++)
64             s2=s2*10+s[i]-'0';
65         sum=tot+s1+s2;
66         s1=s[n-4]-'0';s2=0;
67         for(i=0;i<n-4;i++)
68             s2=s2*10+s[i]-'0';
69         tot+=s1+s2;
70         sum=max(sum,tot);
71         if(n>5)
72         {
73             tot=-(s[n-4]-'0')*(s[n-3]-'0')/((s[n-2]-'0')*10+s[n-1]-'0');
74             s1=s[0]-'0';s2=0;
75             for(i=1;i<n-4;i++)
76                 s2=s2*10+s[i]-'0';
77             tot+=s1+s2;
78             sum=max(sum,tot);
79             tot=-(s[n-4]-'0')*(s[n-3]-'0')/((s[n-2]-'0')*10+s[n-1]-'0');
80             s1=s[n-5]-'0';s2=0;
81             for(i=0;i<n-5;i++)
82                 s2=s2*10+s[i]-'0';
83             tot+=s1+s2;
84             sum=max(sum,tot);
85         }
86         printf("%lld\n",sum);
87     }
88     return 0;
89 }
90 /*
91 //
92 
93 //
94 */
View Code

 

转载于:https://www.cnblogs.com/Coolxxx/p/6011436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值