Vanya and Brackets
2016-04-24 20:45
239 查看
Vanya and Brackets
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 552E
Description
Vanya is doing his maths homework. He has an expression of form
View Code
这道题就是要想明白括号为什么要必须放在两个乘号的之间。
然后因为乘号至多有15个,所以暴力一遍就行了。
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 552E
Description
Vanya is doing his maths homework. He has an expression of form
#include<iostream> #include<stdio.h> using namespace std; char exp[50005]; int t=3; long long cal(int left,int right) { long long now=0; long long ans=0; if(!left) { for(int i=1; i<t-1; i++) { if(exp[i]=='+') { if(now) { ans+=now; now=0; } } else if(exp[i]=='*') { now*=(exp[i+1]-'0'); i++; } else now=exp[i]-'0'; //cout<<now<<endl; } ans+=now; } else { long long tr=0; long long pos=0; for(int i=left+1; i<right; i++) { if(exp[i]=='+') { if(pos) { tr+=pos; pos=0; } } else if(exp[i]=='*') { pos*=(exp[i+1]-'0'); i++; } else pos=exp[i]-'0'; } tr+=pos; //cout<<tr<<endl; for(int i=1; i<t-1; i++) { if(exp[i]=='+') { if(now) { ans+=now; now=0; } } else if(exp[i]=='*') { if(i==left) { now*=tr; i=right-1; } else { now*=(exp[i+1]-'0'); i++; } } else now=exp[i]-'0'; } ans+=now; } return ans; } int main() { while(t) { exp[t++]=getchar(); if(exp[t-1]=='\n') break; } exp[1]='1'; exp[2]='*'; exp[t-1]='*'; exp[t++]='1'; /*for(int i=1;i<t;i++) cout<<exp[i]<<" "; //cout<<cal(0,0);*/ long long ans=cal(0,0); for(int i=1; i<t-1; i++) { if(exp[i]=='*') { for(int j=i+1; j<t-1; j++) { if(exp[i]=='*') { ans=max(ans,cal(i,j)); } } } } printf("%I64d\n",ans); return 0; }
View Code
这道题就是要想明白括号为什么要必须放在两个乘号的之间。
然后因为乘号至多有15个,所以暴力一遍就行了。
相关文章推荐
- python自顶向下的设计方法进行体育竞技分析
- UVA_457题的一些思考
- STL之deque详解
- LeetCode 237. Delete Node in a Linked List C语言
- Android UI基础——ListView性能优化ViewHolder
- 基于POSIX信号量实现生产者消费模型
- 2.3 在maven项目中搭建springmvc中spring
- 使用Safari进行调试
- CodeForces 427B Prison Transfer
- html基础
- hdu 1392(凸包)
- linux命令---删除一个目录下的所有文件,但保留一个指定文件
- 党课心得一(杂谈)
- 转:在Eclipse中关联Android Private Libraries中文件的源代码
- fixed 实现div居中
- markdown 公式实例
- Map与JavaBean之间的相互转化
- NoSQL的理解
- applciation.xml foundation configuration and description
- CodeForces 387A George and Sleep