求助,全部TLE了,初学数据结构

P1449 后缀表达式

mobai4876162 @ 2022-11-19 16:56:11

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXSIZE 100
#define ERROR -1
#define OK 1

typedef struct stack{
    int arr[MAXSIZE];
    int top;
}*Stack;

int Push(int n,struct stack *s);
int Pop(struct stack *s);
int MakeNum(int arr[],int i);

int main(void){
    Stack s = (Stack)malloc(sizeof(struct stack));
    s->top=-1;

    int arr[10];
    int tmp[50];
    int a,b,input;
    int i=0;
    scanf("%c",&input);

    while(input!='@'){
        if(input>='0'&&input<='9'){
            tmp[i]=input;
            i++;
        }
        if(input=='.'){
            Push(MakeNum(tmp,i),s);
            i=0;
        }
        if(input=='+'){
            a=Pop(s);
            b=Pop(s);
            Push(a+b,s);
        }else if(input=='-'){
            a=Pop(s);
            b=Pop(s);
            Push(b-a,s);
        }else if(input=='*'){
            a=Pop(s);
            b=Pop(s);
            Push(a*b,s);
        }else if(input=='/'){
            a=Pop(s);
            b=Pop(s);
            Push(b/a,s); 
        }
        scanf("%c",&input);
    }
    printf("%d",Pop(s));
    return 0;
}

int Push(int n,struct stack *s){
    if(s->top==MAXSIZE-1){
        return ERROR;
    }else{
        s->arr[++(s->top)]=n;
    }
    return OK;
}

int Pop(struct stack *s){
    if(s->top==-1){
        return ERROR;
    }else{
        return s->arr[(s->top)--];
    }
}

int MakeNum(int arr[],int i){

    int num=0;
    while(i){
        num+=((arr[i-1]-48)*pow(10,i-1));
        i--;
    }
    return num;

}

by int_stl @ 2022-12-11 12:28:41

STL里有\texttt{stack}的!

#include <iostream>
#include <stack>
using namespace std;
stack <int> n;
int s=0,x,y;
int main(){
    char ch;
    do{
        ch=getchar();
        if(ch>='0'&&ch<='9'){
            s=s*10+ch-'0';
        }
        else if(ch=='.'){
            n.push(s),s=0;
        }
        else if(ch!='@'){
            x=n.top();n.pop();y=n.top();n.pop();
            switch(ch){
                case '+':n.push(x+y);break;
                case '-':n.push(y-x);break;
                case '*':n.push(x*y);break;
                case '/':n.push(y/x);break;
            }
        }
    }while(ch!='@');
    cout<<n.top();
    return 0;
}

干嘛自己搞一个


by Sesamefox007 @ 2023-01-06 20:40:16

尽量不要使用链式存储的方式解决此类问题,建议使用STL或是数组构造栈进行求解,速度会大大提高


by Habseligkeit @ 2023-01-26 14:15:06


#include<bits/stdc++.h>

using namespace std;

stack<int> num;

void eval(char op) {
    int b = num.top();
    num.pop();
    int a = num.top();
    num.pop();

    if(op == '+') num.push(a + b);
    if(op == '-') num.push(a - b);
    if(op == '*') num.push(a * b);
    if(op == '/') num.push(a / b);

}

int main() {
    string s;
    cin >> s;

    for(int i = 0; i < s.size() - 1; i ++) {
        if(isdigit(s[i])) {
            int j = i , x = 0;
            while(s[j] != '.') x = x * 10 + s[j] - '0' , j ++;
            i = j - 1;
            num.push(x);
        } else if(s[i] != '.') {
            eval(s[i]);
        }
    }

    cout << num.top() << endl;
}

by Habseligkeit @ 2023-01-26 14:17:54

@mobai4876162 非必要尽量不要用链式存储结构,会让程序繁琐许多


|