727 - Equation

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

iqbal csedu
New poster
Posts: 3
Joined: Sun Jun 13, 2010 9:37 pm
Location: CSEDU,Dhaka,Bangladesh
Contact:

Re: 727 - Equation

Post by iqbal csedu » Sun Sep 12, 2010 3:13 pm

ahhhh....alhamdulillah at last got accepted :D :D many many thanks to sazzad vai....have u got my personal message?@sazzad vai
I dream a dream...but my dream is not coming true(still now).

justcodeit
New poster
Posts: 4
Joined: Fri Dec 17, 2010 8:28 am

Re: 727 - Equation

Post by justcodeit » Wed Dec 22, 2010 10:40 am

Please help me....

I m getting runtime error in this problem

i tried eveything......
is STL allowed...

Code: Select all

#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
    long long t,k;
    cin>>t;
    k=t;
    getchar();
    getchar();
    while(t--)
    {
         if(t!=k-1)
             cout<<endl<<endl;
         stack<char> st;
         long long n;
         char infix;
         char postfix[51]="",infix_ex[51]="";
         long long i=0,j;
         while(1)    
         {
              infix=getchar();  
              if(infix=='\n')
                     break;     
              getchar();
              
              infix_ex[i++]=infix;
         }  
         infix_ex[i++]=')';
         n=i;
         st.push('(');
         j=0;
         for(i=0;i<n;i++)
         {
               if( infix_ex[i]=='0' || infix_ex[i]=='1' || infix_ex[i]=='2' || infix_ex[i]=='3' || infix_ex[i]=='4' || infix_ex[i]=='5' || infix_ex[i]=='6' || infix_ex[i]=='7' || infix_ex[i]=='8' || infix_ex[i]=='9' )              
               {
                      postfix[j++]=infix_ex[i];                   
               }
               if(infix_ex[i]=='('  )
               {
                      st.push('(');                     
               }
               
               if(infix_ex[i]=='*' || infix_ex[i]=='/')              
               {
                       while(st.top()=='*' || st.top()=='/')
                       {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;
                       }
                       st.push(infix_ex[i]);
               }
               if(infix_ex[i]=='+' || infix_ex[i]=='-')              
               {
                       while(st.top()=='*' || st.top()=='/' || st.top()=='+' || st.top()=='-')
                       {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;
                       }
                       st.push(infix_ex[i]);
               }
               if(infix_ex[i]==')'  )
               {
                      while(st.top()!='(')
                      {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;                    
                      }     
                      st.pop();               
               }
         }
         cout<<postfix; 
    }
    return 0;
}
please help.....
thanks in advance

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation

Post by brianfry713 » Wed Nov 02, 2011 11:40 pm

justcodeit, you need to check for the input terminating with end of file and print a newline after the last input.
Check input and AC output for thousands of problems on uDebug!

darkurvivor
New poster
Posts: 3
Joined: Tue Oct 25, 2011 5:19 pm

727 - Equation (Runtime Error)

Post by darkurvivor » Wed May 16, 2012 2:18 pm

i've run a lot of input from forum. all the answer is the correct!
but while i submit it returns RE! plz help me to find somewhere wrong!

Code: Select all

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif

    size_t i;
    int n, run=0;
    bool first=true;
    string str;
    cin >> n;

    getchar();
    getline(cin, str);

    while(run < n){
        vector<string> eq;
        stack<char> op;

        if(!first) cout << endl;
        first=false;

        while(getline(cin, str) && str.size()!=0){
            eq.push_back(str);
        }

        for(i=0 ; i<eq.size() ; ++i){

            if(eq[i][0]=='('){
               op.push(eq[i][0]);
            }
            else if(eq[i][0]==')'){
                while(op.top()!='('){
                    cout << op.top();
                    op.pop();
                }
                op.pop();
                if(!op.empty()){
                    if(op.top()=='*'||op.top()=='/'){
                        cout << op.top();
                        op.pop();
                    }
                }
            }
            else if(eq[i][0]<='9' && eq[i][0]>='0'){
                cout << eq[i];
            }
            else{
                if(op.empty() || (!op.empty() && op.top()=='(')){
                   op.push(eq[i][0]);
                }
                else{
                    if(eq[i][0]=='+'||eq[i][0]=='-'){
                        if(op.top()=='*'||op.top()=='/'){
                            cout << op.top();
                            op.pop();
                            if(!op.empty()){
                                cout << op.top();
                                op.pop();
                            }
                        }
                        else{
                            cout << op.top();
                            op.pop();
                        }
                        op.push(eq[i][0]);
                    }
                    else if(eq[i][0]=='*'||eq[i][0]=='/'){
                        if(!op.empty() && (op.top()=='+'||op.top()=='-')){
                            op.push(eq[i][0]);
                        }
                        else{
                            cout << op.top();
                            op.pop();
                            op.push(eq[i][0]);
                        }
                    }
                }
            }
        }
        while(!op.empty()){
            cout << op.top();
            op.pop();
        }
        cout << endl;
        run++;
    }
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation (Runtime Error)

Post by brianfry713 » Wed May 16, 2012 11:34 pm

Next time post in the existing thread. For this input:

Code: Select all

1

1
*
2
(
1
+
2
)
1
+
2
My AC code prints:

Code: Select all

1212+1*2+
Check input and AC output for thousands of problems on uDebug!

darkurvivor
New poster
Posts: 3
Joined: Tue Oct 25, 2011 5:19 pm

Re: 727 - Equation (Runtime Error)

Post by darkurvivor » Thu May 17, 2012 3:44 pm

brianfry713 wrote:Next time post in the existing thread. For this input:

Code: Select all

1

1
*
2
(
1
+
2
)
1
+
2
My AC code prints:

Code: Select all

1212+1*2+
Sorry! I'll post in the exist thread next time.
I run your input and get another answer
but it's not the problem where it is
i found that line 66 lose a statement
while i fixed it as

Code: Select all

if(!op.empty() && op.top()!='(') 
i get AC!!

laituanksa245
New poster
Posts: 20
Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam

727 Equation TLE

Post by laituanksa245 » Tue Jan 01, 2013 8:22 pm

My program passes all the test cases posted in the this forum.
However, I keep getting TLE
Can anyone give me some suggestion how to speed this up ?

Code: Select all

Code Removed
Got Ac :P
Last edited by laituanksa245 on Sun Jan 06, 2013 7:35 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 Equation TLE

Post by brianfry713 » Tue Jan 01, 2013 11:55 pm

Your code is throwing a RE on the sample input.
Check input and AC output for thousands of problems on uDebug!

laituanksa245
New poster
Posts: 20
Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam

Re: 727 Equation TLE

Post by laituanksa245 » Wed Jan 02, 2013 6:11 am

I think the error comes from how my program does input.
How do you recognize a blank line from the input ?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 Equation TLE

Post by brianfry713 » Wed Jan 02, 2013 9:35 pm

Use something like getline, gets, or fgets to read the input.
Check input and AC output for thousands of problems on uDebug!

laituanksa245
New poster
Posts: 20
Joined: Tue Jan 10, 2012 4:23 pm
Location: Vietnam

Re: 727 Equation TLE

Post by laituanksa245 » Thu Jan 03, 2013 5:44 am

Thanks :)

hover1178
New poster
Posts: 3
Joined: Mon Jan 21, 2013 11:57 am

Re: 727 Equation TLE

Post by hover1178 » Mon Jan 21, 2013 12:01 pm

Removed

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

Re: 727 Equation TLE

Post by gr81 » Thu Jan 31, 2013 9:05 am

getting RE, please find the code at http://ideone.com/yvuQZE

Thanks

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

Re: 727 Equation TLE

Post by gr81 » Mon Feb 04, 2013 10:45 am

will someone look into my issue...?

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

Re: 727 Equation TLE

Post by lbv » Wed Feb 06, 2013 12:40 pm

gr81 wrote:getting RE, please find the code at http://ideone.com/yvuQZE
It seems that it's possible that your oper stack gets cleared under some circumstances when it shouldn't, causing the while at line 41 to be skipped (because the stack is empty), but then you pop from oper anyway (line 46), which causes all the troubles.

Check for example this test case:

Code: Select all

1

(
5
/
3
+
5
+
0
)
-
4
*
(
2
)
*
0
/
2
-
5
*
5
/
3
/
6
*
2
+
7
-
9
-
2
/
3

Code: Select all

53/5+0+42*0*2/-55*3/6/2*-7+9-23/-

Post Reply

Return to “Volume 7 (700-799)”