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 ...
随机推荐
- Apple Swift 中文教程 高速參考 基本的语法
总的来说.语法有java的味道,也有python的味道,还有swift自己的味道. 有些语法还是挺不伦不类的,不太好理解,即使你有几年的java或python经验,也不见得有些语法你能非常轻松的看明确 ...
- Android Texting(2)Testing Fundamentals 测试基础篇
Testing Fundamentals The Android testing framework, an integral part of the development environment, ...
- python中的class
尽管Python在Function Programming中有着其他语言难以企及的的优势,但是我们也不要忘了Python也是一门OO语言哦.因此我们关注Python在FP上的优势的同时,还得了解一下P ...
- c++设置输出精度
float类型的精度6-7位,double类型的变量15-16位,但是float占四个字节,double占八个字节, 但能用float类型不要用double因为double占的字节数多,而且运算速度要 ...
- gcc支持c99验证
gcc3.0以上的版本都是支持C99标准的, 但是编译程序的时候需要加上 -std=c9 才可以: 一下程序是验证gcc是否支持c99标准的: #include <stdio.h> ...
- [Swust OJ 603]--吃饺子大王
题目链接:http://acm.swust.edu.cn/problem/603/ Time limit(ms): 1000 Memory limit(kb): 65535 同学们还记得我们班 ...
- Oracle修改字段长度以及计算天数
sql修改字段长度的语法: alter table 表名 modify 字段名 字段类型; sql修改字段长度的示例代码 alter table qtline modify qtl_bidernote ...
- 432B - Football Kit
解题思路: 暴力绝对TLE 一个队伍穿主场球衣的次数 = 这个队伍的客场球衣颜色与其他队主场球衣颜色起冲突的次数 + (n - 1) #include <stdio.h> #include ...
- 进入MFC讲坛的前言(一)
在这里,我想谈谈自己学习MFC的一些体会.我是从1997年才开始在Window下编写程序的.在这之前,我编写过一些DOS程序,包括一个简单的全屏幕编辑器和一个带函数的表达式解释器,都是一些小的程序.W ...
- 关于ActionContext.getContext()的使用方法心得
这个也是我在另外一位仁兄的博客中看到的,原博客的有点长,我把它精简了一下,算看起来比較方便吧. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSe ...