4 solutions
-
2
思路
和上一道题一样,只不过这道题需要倒序进去倒序输出就ok了
#include<bits/stdc++.h> using namespace std; char a[10000]; char b[10000];//存一下转成后缀输出的,然后再倒序输出 int main() { stack <char> s; cin>>a; int num=0; for(int i=strlen(a)-1;i>=0;i--){ if(a[i]<='9'&&a[i]>='0'){ b[num]=a[i]; num++; } else if(a[i]=='+'||a[i]=='-'){ if(s.empty()){ s.push(a[i]); } else{ while(!s.empty()&&s.top()!=')'){ b[num]=s.top(); s.pop(); num++; } s.push(a[i]); } } else if(a[i]=='*'||a[i]=='/'){ if(!s.empty()){ while(!s.empty()&&s.top()=='*'||s.top()=='/'){ b[num]=s.top(); num++; s.pop(); } s.push(a[i]); } else{ s.push(a[i]); } } else if(a[i]==')'){ s.push(a[i]); } else if(a[i]=='('){ while(!s.empty()&&s.top()!=')'){ b[num]=s.top(); num++; s.pop(); } s.pop(); } } while(!s.empty()){ b[num]=s.top(); num++; s.pop(); } for(int i=num-1;i>=0;i--){ cout<<b[i]; } return 0; }
Information
- ID
- 295
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 4
- Tags
- # Submissions
- 39
- Accepted
- 19
- Uploaded By