链接:http://vjudge.net/problem/viewProblem.action?id=21358

描述:给出一个算式,算式里面有加法和乘法,可以任意添加括号从而改变计算顺序。求可能得到的最大结果和最小结果。

思路:贪心

求最大结果:尽量让乘数最大,所以先把所有加法做完,然后再做乘法。

求最小结果:尽量让乘数最小,所以就优先做乘法,这样乘数就只是'*'相邻的两个数。

我的实现(高精度版):

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define MaxLen 100
6 #define hp_Len 100
7 typedef int hp[hp_Len];
8 char str[MaxLen];
9 int n,Len;
10 hp ans_Max,ans_Min;
11 void hp_Add(hp a,hp b,hp &ans)
12 {
13 hp c;
14 int i,len=max(a[0],b[0])+3;
15 memset(c,0,sizeof(c));
16 if(a[0]>=b[0])
17 {
18 for(i=1;i<=b[0];++i)
19 {
20 c[i]+=a[i]+b[i];
21 c[i+1]+=c[i]/10;
22 c[i]%=10;
23 }
24 for(;i<=a[0];++i)
25 {
26 c[i]+=a[i];
27 c[i+1]+=c[i]/10;
28 c[i]%=10;
29 }
30 }
31 else
32 {
33 for(i=1;i<=a[0];++i)
34 {
35 c[i]+=a[i]+b[i];
36 c[i+1]+=c[i]/10;
37 c[i]%=10;
38 }
39 for(;i<=b[0];++i)
40 {
41 c[i]+=b[i];
42 c[i+1]+=c[i]/10;
43 c[i]%=10;
44 }
45 }
46 while(len>1&&c[len]==0)
47 len--;
48 c[0]=len;
49 memcpy(ans,c,sizeof(c));
50 }
51 void hp_Mul(hp a,hp b,hp &ans)
52 {
53 hp c;
54 int i,j,len=a[0]+b[0]+5;
55 memset(c,0,sizeof(c));
56 for(i=1;i<=a[0];++i)
57 for(j=1;j<=b[0];++j)
58 c[i+j-1]+=(a[i]*b[j]);
59 for(i=1;i<=len;++i)
60 {
61 c[i+1]+=c[i]/10;
62 c[i]%=10;
63 }
64 while(len>1&&c[len]==0)
65 len--;
66 c[0]=len;
67 memcpy(ans,c,sizeof(c));
68 }
69 inline void Put_in_hp(hp &ans,int a)
70 {
71 hp c;
72 memset(c,0,sizeof(c));
73 while(a)
74 {
75 c[0]++;
76 c[c[0]]=a%10;
77 a/=10;
78 }
79 memcpy(ans,c,sizeof(c));
80 }
81 void Get_Max()
82 {
83 Len+=2;
84 str[Len-2]='*';str[Len-1]='1';
85 int i,tmp=0;
86 hp Pre,Cur;
87 Pre[0]=1;Pre[1]=0;//Pre=0;
88 ans_Max[0]=1;ans_Max[1]=1;//ans_Max=1;
89 for(i=0;i<Len;++i)
90 {
91 if('0'<=str[i]&&str[i]<='9')
92 {
93 tmp=tmp*10+str[i]-'0';
94 continue;
95 }
96 Put_in_hp(Cur,tmp);
97 hp_Add(Pre,Cur,Pre);//Pre+=Cur;
98 if(str[i]=='*')
99 {
100 hp_Mul(ans_Max,Pre,ans_Max);//ans_Max*=Pre;
101 Pre[0]=1;Pre[1]=0;//Pre=0;
102 }
103 tmp=0;
104 }
105 }
106 void Get_Min()
107 {
108 str[Len-2]='+';str[Len-1]='0';
109 int i,tmp=0;
110 hp Pre,Cur;
111 Pre[0]=1;Pre[1]=1;//Pre=1;
112 ans_Min[0]=1;ans_Min[1]=0;//ans_Min=0;
113 for(i=0;i<Len;++i)
114 {
115 if('0'<=str[i]&&str[i]<='9')
116 {
117 tmp=tmp*10+str[i]-'0';
118 continue;
119 }
120 Put_in_hp(Cur,tmp);
121 hp_Mul(Pre,Cur,Pre);//Pre*=Cur;
122 if(str[i]=='+')
123 {
124 hp_Add(ans_Min,Pre,ans_Min);//ans_Min+=Pre;
125 Pre[0]=1;Pre[1]=1;//Pre=1;
126 }
127 tmp=0;
128 }
129 }
130 void hp_Print(hp c)
131 {
132 int i;
133 for(i=c[0];i>=1;--i)
134 printf("%d",c[i]);
135 }
136 inline void Print()
137 {
138 printf("The maximum and minimum are ");
139 hp_Print(ans_Max);
140 printf(" and ");
141 hp_Print(ans_Min);
142 printf(".\n");
143 }
144 int main()
145 {
146 scanf("%d",&n);
147 for(int i=1;i<=n;++i)
148 {
149 scanf("%s",str);
150 Len=strlen(str);
151 Get_Max();
152 Get_Min();
153 Print();
154 }
155 return 0;
156 }

实现二(long long版,之前因为%I64d的原因WA了,改成%lld就AC了):

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define MaxLen 100
6 char str[MaxLen];
7 typedef long long llt;
8 llt n,Len,ans_Max,ans_Min;
9 void Get_Max()
10 {
11 Len+=2;
12 str[Len-2]='*';str[Len-1]='1';
13 llt i;
14 llt Pre=0,Cur=0;
15 ans_Max=1;
16 for(i=0;i<Len;++i)
17 {
18 if('0'<=str[i]&&str[i]<='9')
19 {
20 Cur=Cur*10+str[i]-'0';
21 continue;
22 }
23 Pre+=Cur;
24 if(str[i]=='*')
25 {
26 ans_Max*=Pre;
27 Pre=0;
28 }
29 Cur=0;
30 }
31 }
32 void Get_Min()
33 {
34 str[Len-2]='+';str[Len-1]='0';
35 llt i;
36 llt Pre=1,Cur=0;
37 ans_Min=0;
38 for(i=0;i<Len;++i)
39 {
40 if('0'<=str[i]&&str[i]<='9')
41 {
42 Cur=Cur*10+str[i]-'0';
43 continue;
44 }
45 Pre*=Cur;
46 if(str[i]=='+')
47 {
48 ans_Min+=Pre;
49 Pre=1;
50 }
51 Cur=0;
52 }
53 }
54 int main()
55 {
56 scanf("%I64d",&n);
57 for(int i=1;i<=n;++i)
58 {
59 scanf("%s",str);
60 Len=strlen(str);
61 Get_Max();
62 Get_Min();
63 printf("The maximum and minimum are %I64d and %I64d.\n",ans_Max,ans_Min);
64 }
65 return 0;
66 }

[题解]UVA10700 Camel trading的更多相关文章

  1. UVA10700:Camel trading(栈和队列)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/J 题目大意: 给一个没有加上括号的表达式且只有+ , ...

  2. UVA 10700 Camel trading 无括号的表达式 贪心

    题意:给出只包含数字和+*的表达式,你可以自己安排每一个运算的顺序,让你找出表达式可能得到的最大值和最小值. 很明显,先乘后加是最小值,先加后乘能得到最大值. 其实不是很明显... 证明下: 数字的范 ...

  3. uva:10700 - Camel trading(贪婪)

    题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这种表达式加上括号后能得到的最大值和最小值. ...

  4. UVa 10700 - Camel trading

    题目大意:给一个不含括号.只有+和*运算的表达式,数字的范围在1到20之间,算出计算结果的可能最大值和最小值. 贪心,如果加法优先级比乘法高,那么得出的结果为最大值.(a+b)*c = a*c + b ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  7. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  8. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem L. Stock Trading Robot 水题

    Problem L. Stock Trading Robot 题目连接: http://www.codeforces.com/gym/100253 Description CyberTrader is ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

随机推荐

  1. mysql数据库优化1

    目录 数据库结构的设计优化 1.数据库结构的设计 2.针对大型的数据量提前进行分库和分表 3.分库分表带来的问题 4.表结构设计注意的问题 查询优化 1.查询语句的注意事项 2.应尽量避免在 wher ...

  2. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  3. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  4. golang中结构体的嵌套、方法的继承、方法的重写

    package main import "fmt" type human struct { name, phone string age int8 } type student s ...

  5. Mac中显示及隐藏文件和文件夹的方法

    一.方法一 直接在文件或文件夹名前面的加一个'.'点号,然后系统会弹出修改确认对话框,点好就行了. 隐藏文件 解除隐藏可以通过方法三显示所有隐藏文件,找到该文件去掉开头的'.',然后通过方法二来解除隐 ...

  6. python 求模运算符--判断奇偶数

    #!/usr/bin/python #coding=utf-8 #好好学习,天天向上 number = input("please enter a number:") number ...

  7. 利用Jemalloc进行内存泄漏的调试

    内存不符预期的不断上涨,可能的原因是内存泄漏,例如new出来的对象未进行delete就重新进行复制,使得之前分配的内存块被悬空,应用程序没办法访问到那部分内存,并且也没有办法释放:在C++中,STL容 ...

  8. python_f-string格式化字符串文字

    一.简介 f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法. f-string在形式上是以 f 或 F ...

  9. 异步回调实现- Guava Retryer

    为什么要使用重试利器Retryer 在实际开发中我们经常会遇到需要轮询查询一个接果,实现轮询的方式有很多种,我们经常要写许多代码,有时还会怕写出的代码有bug,如果已经有轮子了,我们就没必要重复造轮子 ...

  10. 7月3日下午 微擎芸众商城 设计思路 - laravel路由底层源码解读

    学习参考文章 https://learnku.com/articles/13622/the-principle-of-laravel-routing-execution <?phpnamespa ...