- 题解
- 8 年前 @
好像能用***双栈***做,但是我不会……求各路大神指教。根据***运算符优先级***进行运算的好像
2 条评论
-
ComeOrDream LV 8 @ 8 年前
为什么要用双栈
-
8 年前@
一个操作数栈,一个操作符栈。
- 1
#include<cstdio>
#include<cstring>
#define mod 10000
#define maxn (1000000 + 10)
#define f(x,y) for( register int x = 1;x <= y;x ++)
using namespace std;
char c[maxn];
int st[maxn];
int pos = 0;
#define top st[pos]
#define pop1() pos--
int main() {
scanf("%s",c + 1);
int len = strlen(c + 1);
int temp = 0;
f(i,len+1) {
if (c[i] >= '0' && c[i] <= '9') temp = (temp * 10 + c[i] - '0') % mod;
else {
if (top == -2) {
pop1();
int t = (top * temp) % mod;
st[pos] = t;
}else st[++ pos] = temp;
if(c[i] == '*') st[++ pos] = -2;
temp = 0;
}
}
int ans = 0;
pos ++;
while(pos --) ans = (ans + st[pos]) % mod;
printf ("%d",ans);
return 0;
}
为什么要用双栈