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的更多相关文章

  1. HDU 3350 #define is unsafe

    题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...

  2. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  3. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  4. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. uva 10891 Game of Sum(区间dp)

    题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...

  2. 在不连接网线的情况下Windos与VM之间如何ping通

    一般情况下,如果宿主主机的网口连接网线并且能够上网,那么按照VM的默认安装,在VM-Settings-Hardware-Network Adapter-Network connection中选择Bri ...

  3. C#_会员管理系统:开发二(会员资料管理界面的‘增删改查’)

    会员资料管理界面: 新建一个窗体,窗体界面和控件如下: 窗体中的控件dgvManager更改FullRowSelect属性(点击选中效果)为:FullRowSelect 会员资料管理界面窗体的详细代码 ...

  4. Ubuntu设置为命令行登录

    root@ubuntu:~# vi /etc/default/grub 改: #GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" GRUB_CMD ...

  5. DFS 练习 (这篇真的是随笔)

    目的: 输入: 3 输出: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 代码如下: #include<stdio.h> ],b[],n; void dfs(in ...

  6. django学习之Model(三)QuerySet

    接下来主要学习Models中的Making queries 写好models.py后,django会自动提供一个数据库的抽象API,来实现CRUD(create, retrieve, update, ...

  7. Python 做过哪些有趣的项目

          1 icedx   241 天前 via Android   ♥ 1 考虑到Windows 下的类Alfred 软件都太傻逼 自己用PyQT 写了一个       2 crazyxin19 ...

  8. jquery分页

    //分页插件 /** 2015-12-7 **/ (function($){ var ms = { init:function(obj,args){ return (function(){ ms.fi ...

  9. BZOJ 4057: [Cerc2012]Kingdoms( 状压dp )

    状压dp.... 我已开始用递归结果就 TLE 了... 不科学啊...我dp基本上都是用递归的..我只好改成递推 , 刷表法 将全部公司用二进制表示 , 压成一个数 . 0 表示破产 , 1 表示没 ...

  10. Linux开机启动十步骤

    启动第一步--加载BIOS 启动第二步--读取MBR 启动第三步--Boot Loader 启动第四步--加载内核 启动第五步--用户层init依据inittab文件来设定运行等级 启动第六步--in ...