4 solutions

  • 2
    @ 2022-1-8 16:59:16

    思路

    和上一道题一样,只不过这道题需要倒序进去倒序输出就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;
    }
    
    
    • 2
      @ 2022-1-3 20:53:01

      使用两个栈的做法:

      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      	char i[1005];
      	scanf("%s",i);
      	int k=strlen(i);
      	stack<char> pp;
      	stack<char> dd;
       	for(int c=k-1;c>=0;c--){
      		int l=i[c]-'0';
      		if(l>=0&&l<=9) pp.push(i[c]);
      		else{
      			if(dd.empty()) dd.push(i[c]);
      			else if(i[c]==')') dd.push(i[c]);
      			else if(i[c]=='*'||i[c]=='/'){
      				dd.push(i[c]);
      			}else if(i[c]=='-'||i[c]=='+'){
      				while(dd.top()!='-'&&dd.top()!='+'&&dd.top()!=')'){
      					pp.push(dd.top());
      					dd.pop();
      					if(dd.empty()) break;
      					if(dd.top()==')') break;
      				}
      				dd.push(i[c]);
      			}else if(i[c]=='('){
      				while(dd.top()!=')'){
      					pp.push(dd.top());
      					dd.pop();
      				}
      				dd.pop();
      			}
      		}
      	}
      	while(!dd.empty()){
      		pp.push(dd.top());
      		dd.pop();
      	}
      	while(!pp.empty()){
      		cout<<pp.top();
      		pp.pop();
      	}
      	return 0;
      }
      
      • 0
        @ 2022-1-26 19:56:34

        和上道题差不多,不过改一下判断条件

        #include<iostream>
        #include<cstring>
        #include<stack>
        using namespace std;
        const int N=1007;
        int f=99999;
        char a[N];
        stack<char>s1;
        stack<char>s2;
        int main(){
        	gets(a);
        	int n=strlen(a);
        		for(int i=n-1;i>=0;i--){
        		int k=9;
        		if(a[i]=='*')k=1;
        		if(a[i]=='/')k=1;
        		if(a[i]=='+')k=0;
        		if(a[i]=='-')k=0;
        		if(a[i]=='(')k=3;
        		if(a[i]==')')k=3;
        		if(a[i]>='0'&&a[i]<='9')
        			s1.push(a[i]);
        		else if(k==1||k==0){
        			if(s2.empty())
        				s2.push(a[i]);
        			else {
        				if(s2.top()==')'||k<=f){
        					s2.push(a[i]);
        				}
        				else {
        					while(1){
        						if(s2.empty())
        							break;
        						if(s2.top()==')')
        							break;
        						s1.push(s2.top());
        						s2.pop();
        					}
        					s2.push(a[i]);
        				}
        			}
        		}
        		else if(k==3){
        			if(a[i]=='('){
        				while(s2.top()!=')'){
        					s1.push((s2.top()));
        					s2.pop();
        				}
        				s2.pop();
        			}
        			else s2.push(a[i]);
        		}
        		f=k;
        	}
        	while(!s2.empty()){
        		s1.push((s2.top()));
        		s2.pop();
        	}
        	while(!s1.empty()){
        		cout<<s1.top();
        		s1.pop();
        	}
        	return 0;
        }
        
        • 0
          @ 2022-1-13 22:03:45
          #include<stdio.h>
          #include<string.h>
          #include<stack>
          #include<queue>
          using namespace std;
          int main()
          {
            char arr2[1000];
            char arr[1000];
            scanf("%s",arr);
            stack<char>room;
            int j = 0;
            for(int i = strlen(arr) - 1; i >= 0; i--)
            {
            	if(arr[i] >= 48 && arr[i] <= 57)
            	{
            		arr2[j] = arr[i];
            		j ++;
          	  }
          	else if(arr[i] == ')' || arr[i] == '(')
          	{
          		if(arr[i] == ')')room.push(arr[i]);
          		else
          		{
          			while(room.top() != ')')
          	  	    {
          	  		if(room.size() == 0)break;
          	  		arr2[j] = room.top();
          	  		j ++;
          			room.pop();
          		    }
          		room.pop();
          		}	
          	}
          	else
          	{
          		if(room.size() == 0)
          		{
          			room.push(arr[i]);
          		}
          		else
          		{
          			if(arr[i] == '*' || arr[i] == '/')room.push(arr[i]);
          			else if(arr[i] == '+' )
          			{
          				if(room.top() == '+' || room.top() == '-'||room.top() == ')')room.push(arr[i]);
          				else
          				{
          						while(room.size() != '+' || room.top() != '-')
          						{
          						   arr2[j] = room.top();
          						   j ++;
          						    room.pop();
          							if(room.size() == 0)break;	
          						}
          						 room.push(arr[i]);
          				}
          			}
          			else
          			{
          				if(room.top() == '-' || room.top() == '(')room.push(arr[i]);\
          				else
          				{
          					while(room.top() != '-'&&room.top() != ')')
          					{
          						arr2[j] = room.top();
          						j ++;
          						room.pop();
          				  		if(room.size() == 0)break;
          					}
          					room.push(arr[i]);
          				}
          			}
          		}
          	}
          	}
          	 while(room.size() != 0)
          	 {
          	 	arr2[j] = room.top();
          	 	j++;
          	 	room.pop();
          	 	if(room.size() == 0)break;
          	 }
          	 while(j--)
          	 {
          	 	printf("%c",arr2[j]);
          	 }
            return 0;
          }
          
          • 1

          Information

          ID
          295
          Time
          1000ms
          Memory
          128MiB
          Difficulty
          4
          Tags
          # Submissions
          39
          Accepted
          19
          Uploaded By