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 ...
随机推荐
- Failed to load the JNI shared library
解决Eclipse无法打开"Failed to load the JNI shared library" 这是由于JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的w ...
- c++中的对象引用(object reference)与对象指针的区别
★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指针需 ...
- window下如何搭建linux环境
1.使用虚拟机 使用VMware虚拟机,下载linux内核系统,加载运行. 2.cygwin 安装cygwin,设置环境变量. 第二种方法还是比较简便的.优先考虑.
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】
这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小 根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的 Because the total ...
- USACO Arithmetic Progressions 【构造等差数列】
USER: Jeremy Wu [wushuai2] TASK: ariprog LANG: C++ Compiling... Compile: OK Executing... Test 1: TES ...
- java的new BufferedReader(new InputStreamReader(System.in))
流 JAVA /IO 基本小结 通过一行常见的代码讨论:new BufferedReader(new InputStreamReader(System.in)) /*** *** 看到这篇文章挺好的, ...
- poj2947
高斯消元法模版题,但套模版没用.. 先回顾一下线性代数的知识. 若要求解如下方程: 首先,其系数矩阵为 然后,其增广矩阵为: 然后若要求解这个方程,首先将第一行第一个元素化为1,即:第一行乘以1/3. ...
- Axis2(10):使用soapmonitor模块监视soap请求与响应消息
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...
- 理光C5502A 打印模糊问题
1.这款打印机好几W,我来的时候就有了.挺高端的. 2.来的时候由于网络没建成.建成之后,全部设置成网络打印机. 3.可以扫描成jpg\pdf,并且可以通过共享设置成扫描到目的地. 4.还有其它一些功 ...