hdu 3350
题意:让你求运算式的结果和运算过程中加法的次数
(a) > (b) ? (a) : (b) 大于取a,小于等于取b
MAX( 1 + 2 , 3) 因为(a) > (b) ? (a) : (b) 所以取后面的值而在比较时进行了一次加法运算所以加法运算只有一次
MAX(3,1+2) 依旧取后面的值比较时进行一次加法运算,取后面的值还要进行一次加法运算,所以加法运算一共有两次
题解:数据结构的典型应用,两个栈一个存符号位,一个存数字,遇到')'进行一次运算
实现在数字栈中压入0,符号栈压入' ( ' , ' , ' 相当于预设的一次,最后再压入 ')' 进行最后的结果运算
总之,细心就能做出来吧……
自己还是太差了……!!!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <queue>
#include <map>
#include <set> using namespace std; const int INF = 0x7ffffff;
const double ESP = 10e-;
const double Pi = * atan(1.0);
const int MAXN = + ;
const long long MOD = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,};
typedef long long LL; LL gac(LL a,LL b){
return b?gac(b,a%b):a;
}
char str[MAXN];
struct Point{
int num;
int cnt;
Point(int x = ,int y = ):num(x),cnt(y){}
};
stack<Point>s1;
stack<char>s2;
int main(){
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
int t;
while(~scanf("%d",&t)){
while(t--){
scanf("%s",str);
while(!s1.empty()){
s1.pop();
}
while(!s2.empty()){
s2.pop();
}
s1.push(Point(,));
s2.push('(');
s2.push(',');
int len = strlen(str);
/*!!!MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))*/
for(int i = ;i <= len;i++){
if(i == len){
str[i] = ')';
str[len+] = '\0';
}
if(isalpha(str[i])){
s2.push('(');
i += ;
}
else if(isdigit(str[i])){
int j = i;
int num = ;
while(str[j] >= '' && str[j] <= ''){
num = num * + str[j] - '';
j++;
}
i = j-;
s1.push(Point(num,));
}
else if(str[i] == ',' || str[i] == '+'){
s2.push(str[i]);
}
else if(str[i] == ')'){
char ch = s2.top();
if(ch == '('){
s2.pop();
continue;
}
Point a = s1.top();
int num1 = a.num;
int cnt1 = a.cnt;
s1.pop();
while(ch == '+'){
s2.pop();
ch = s2.top();
Point b = s1.top();
s1.pop();
cnt1 += b.cnt;
cnt1++;
num1 += b.num;
}
s2.pop();
a = s1.top();
int num2 = a.num;
int cnt2 = a.cnt;
s1.pop();
ch = s2.top();
while(ch == '+'){
s2.pop();
ch = s2.top();
Point b = s1.top();
s1.pop();
cnt2 += b.cnt;
cnt2++;
num2 += b.num;
}
int tt = ;
if(num2 > num1){
tt = cnt2;
if(i != len)
tt *= ;
tt += cnt1;
s1.push(Point(num2,tt));
}
else{
tt = cnt1;
if(i != len)
tt *= ;
tt += cnt2;
s1.push(Point(num1,tt));
}
s2.pop();
}
}
printf("%d %d\n",s1.top().num,s1.top().cnt);
}
}
return ;
}
hdu 3350的更多相关文章
- HDU 3350 #define is unsafe
题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- splice()函数的使用方法
splice()函数的使用方法,这是一个拗口的函数.用起来有点麻烦.图3所看到的是splice函数的功能.将一个列表插入到还有一个列表其中.list容器类定义了splice()函数的3个版本号: sp ...
- web.xml 中<taglib>报错(转载)
在web.xml加入taglib <taglib> <taglib-uri>/WEB-INF/tiles.tld</taglib- uri> <taglib- ...
- 利用VC助手(VA)添加注释
利用VC助手(VA)添加注释 今天想给自己写的代码加上版权信息,同时整理一下代码的注释.但是为了保持同样的格式,总是copy,显得有些繁琐.然后试图找解决方案.我用的是VS 2010, 刚开始是尝试了 ...
- spring-mvc关键点掌握 high level
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- Week8(10月28日)
Part I:提问 =========================== 1. Lazy.Eager.Explicit Loading的关键字是什么? 2. 请比较三种加载方式的性能. Part ...
- jq商品展示图放大镜 and 原生js和html5写的放大镜效果 ~~效果不错
<!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8&qu ...
- 练习 jquery+Ajax+Json 绑定数据 分类: asp.net 练习 jquery+Ajax+Json 绑定数据 分类: asp.net
练习 jquery+Ajax+Json 绑定数据
- mybatis-redis项目分析
redis作为现在最优秀的key-value数据库,非常适合提供项目的缓存服务.把redis作为mybatis的查询缓存也是很常见的做法.在网上发现N多人是自己做的Cache,其实在mybatis的g ...
- setitimer()函数使用
setitimer()为Linux的API,并非C语言的Standard Library,setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个 ...
- SQL__用命令删除定期的备份数据库文件
用计划任务可以定期执行下列语句: FORFILES /P e:\test /M *.bak /C "cmd /C del /Q @path" /d -4 其中可更换目录与文件类型. ...