社区讨论

蒟蒻求助!!

P1449后缀表达式参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mi7xoujo
此快照首次捕获于
2025/11/21 05:19
4 个月前
此快照最后确认于
2025/11/21 05:19
4 个月前
查看原帖
为什么在本机跑所有数据都能过但是交上去样例都WA了...
代码:
CPP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<cmath>
using namespace std;
stack<char> tmp;
bool check(char a){
    if(a=='+'||a=='-'||a=='*'||a=='/')
        return true;
    else
        return false;
    
}
int fun(int a,int b,char c){
    switch (c)
    {
        case '+':
            return a+b;
        case '-':
            return b-a;
        case '*':
            return a*b;
        case '/':
            return (int)(b/a);
    }
}
int gen(int l){
    int ans,s,c;
    char temp;
    for(int k=0;k<l;k++){
        s=(int)pow(10,k);
        temp=tmp.top();
        tmp.pop();
        c=(int)temp-48;
        ans+=c*s;
    }
    return ans;
}
int main(){
    stack<int> num;
    string str;
//    memset(tmp,0,sizeof(tmp));
    cin>>str;
    int size=str.size();
    int t1,t2;
    for(int i=0;i<size;i++){
        if(str[i]=='@'){
            printf("%d",num.top());
            return 0;
        }
        else if(str[i]!='.'&&!check(str[i]))
            tmp.push(str[i]);
        else if(str[i]=='.')
            num.push(gen(tmp.size()));
        else if(check(str[i])){
            t1=num.top();
            num.pop();
            t2=num.top();
            num.pop();
            num.push(fun(t1,t2,str[i]));
        }
    }
    return 0;
}
望大佬指教

回复

1 条回复,欢迎继续交流。

正在加载回复...