hdu 3350
阅读原文时间:2025年03月12日阅读:1

hdu 3350

题意:让你求运算式的结果和运算过程中加法的次数

  (a) > (b) ? (a) : (b) 大于取a,小于等于取b

  MAX( 1 + 2 , 3) 因为(a) > (b) ? (a) : (b) 所以取后面的值而在比较时进行了一次加法运算所以加法运算只有一次

  MAX(3,1+2) 依旧取后面的值比较时进行一次加法运算,取后面的值还要进行一次加法运算,所以加法运算一共有两次

题解:数据结构的典型应用,两个栈一个存符号位,一个存数字,遇到')'进行一次运算

  实现在数字栈中压入0,符号栈压入' ( '  ,  ' , ' 相当于预设的一次,最后再压入 ')' 进行最后的结果运算

  总之,细心就能做出来吧……
  自己还是太差了……!!!

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

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){}
};
stacks1;
stacks2;
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 ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章