社区讨论
求检查代码
P1981[NOIP 2013 普及组] 表达式求值参与者 6已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mi4f1nlz
- 此快照首次捕获于
- 2025/11/18 18:13 4 个月前
- 此快照最后确认于
- 2025/11/18 18:13 4 个月前
CPP
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
char s[300];
int i;
stack<int> num;
stack<char> sym;
void calc()
{
int a=num.top();
num.pop();
int b=num.top();
num.pop();
switch(sym.top())
{
case '*':num.push(a*b);break;
case '/':num.push(a/b);break;
case '+':num.push(a+b);break;
case '-':num.push(a-b);break;
}
sym.pop();
}
bool comp()
{
if(sym.top()=='*'||sym.top()=='/') return 1;
if(s[i]=='*'||s[i]=='/') return 0;
if((s[i]=='+'||s[i]=='-')&&sym.top()=='(') return 0;
return 1;
}
int main()
{
gets(s);
sym.push('(');
for(i=0;i<=strlen(s)-1;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
int x=0;
while(s[i]>='0'&&s[i]<='9')
x=x*10+s[i++]-'0';
i--;
num.push(x);
continue;
}
while(comp()) calc();
sym.push(s[i]);
}
while(sym.top()!='(')
calc();
printf("%d\n",num.top()%10000);
return 0;
}
样例都可以过。。自己做的测试点也miu问题。。但是就是20分。。求检查
回复
共 5 条回复,欢迎继续交流。
正在加载回复...