icpc:problems:luogup1981
problems | |
---|---|
名称 | 表达式求值 |
题目编号 | 2013 NOIP PJ T2 |
题目链接 | luogu.com.cn/… |
来源 | CCF |
算法分类 | 栈, 字符串 |
难易程度 | 容易 |
表达式求值
想法
使用栈记录加法的数,遇到乘法进行标记,在读入完后一个数后进行合并。字符串读入后处理栈内的所有数。
代码实现
#include<cstdio> const int N = 100010; long long int sta[N]; int top; int main() { char ch; bool flag = false; while(~scanf("%c", &ch)) { if(ch >= '0' && ch <= '9') { sta[top] = sta[top]*10 + ch - '0'; } else { if(flag) { sta[top-1] *= sta[top]; sta[top-1] %= 10000; sta[top] = 0; flag = false; top--; } top++; if(ch == '*') flag = true; } } while(top > 0) { sta[top-1] = (sta[top-1] + sta[top]) % 10000; top--; } printf("%lld", sta[0]); return 0; }
/app/www/public/data/pages/icpc/problems/luogup1981.txt · 最后更改: 2024/01/29 12:17 由 温婕莺