璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


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 由 温婕莺