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. rbd块映射

    rbd块映射: root@u18:~# rbd create kvm/test002.img --size root@u18:~# rbd info kvm/test002.img rbd image ...

  2. 【Eclipse】修改java代码不强制重启

    找到tomcat的server.xml文件,修改以下代码,重新发布重启.然后修改java代码就可以不用重启了. 将reloadable=“true”改成reloadable="false&q ...

  3. 字符设备驱动4: ioremap

    #define GPIO_OFT(x) ((x) - 0x56000000)#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT ...

  4. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  5. php 学习笔记 数组2

    10.切割数组 array_slice(array, offset, length);返回一个由原始数组中的连续元素组成的新数组,参数1为原始数组,参数2为要复制的起始位置, 参数3要复制的个数:新数 ...

  6. 如何用 new 来动态开辟一个二维数组

    一般的做法是: int **p = new int*[m]; //m行n列型 for (i = 0; i < m; ++i) { p[i] = new int[n]; for (j = 0; j ...

  7. ZOJ 2853 Evolution 【简单矩阵快速幂】

    这道题目第二次看的时候才彻底理解了是什么意思 把题目转化为数学模型分析后就是 有一个初始序列, 有一个进化率矩阵 求的是初始序列 与进化率矩阵进行 m 次运算后, 初始序列最后一位的答案 那么显然,可 ...

  8. php 解析url 和parse_url使用

    通过url进行传值,是php中一个传值的重要手段.所以我们要经常对url里面所带的参数进行解析,如果我们知道了url传递参数名称,例如 /index.php?name=tank&sex=1#t ...

  9. java实现从实体到SQL语句的转换(二)

    前面写过一篇生成insert语句的博客,今天把CRUD的都完善了一下,需要的上次写的获取实体信息的类,这里就不贴出来了[点击打开查看] 下面是完整的代码: package tan.code.utils ...

  10. Debian为程序添加一个开始菜单,debian添加sublime开始菜单.

    下了一个 '绿色' 的程序,想要加到开始菜单里面. 怎么做呢? 我这里以sublime2做例 去http://www.sublimetext.com/2 下载了linux 64位, 解压放到了下面的文 ...