727 - Equation
Moderator: Board moderators
-
- New poster
- Posts: 3
- Joined: Sun Jun 13, 2010 9:37 pm
- Location: CSEDU,Dhaka,Bangladesh
- Contact:
Re: 727 - Equation
ahhhh....alhamdulillah at last got accepted 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).
-
- New poster
- Posts: 4
- Joined: Fri Dec 17, 2010 8:28 am
Re: 727 - Equation
Please help me....
I m getting runtime error in this problem
i tried eveything......
is STL allowed...
please help.....
thanks in advance
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;
}
thanks in advance
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 - Equation
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!
-
- New poster
- Posts: 3
- Joined: Tue Oct 25, 2011 5:19 pm
727 - Equation (Runtime Error)
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!
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 - Equation (Runtime Error)
Next time post in the existing thread. For this input:
My AC code prints:
Code: Select all
1
1
*
2
(
1
+
2
)
1
+
2
Code: Select all
1212+1*2+
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 3
- Joined: Tue Oct 25, 2011 5:19 pm
Re: 727 - Equation (Runtime Error)
Sorry! I'll post in the exist thread next time.brianfry713 wrote:Next time post in the existing thread. For this input:My AC code prints:Code: Select all
1 1 * 2 ( 1 + 2 ) 1 + 2
Code: Select all
1212+1*2+
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()!='(')
-
- New poster
- Posts: 20
- Joined: Tue Jan 10, 2012 4:23 pm
- Location: Vietnam
727 Equation TLE
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 ?
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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 Equation TLE
Your code is throwing a RE on the sample input.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 20
- Joined: Tue Jan 10, 2012 4:23 pm
- Location: Vietnam
Re: 727 Equation TLE
I think the error comes from how my program does input.
How do you recognize a blank line from the input ?
How do you recognize a blank line from the input ?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 Equation TLE
Use something like getline, gets, or fgets to read the input.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 20
- Joined: Tue Jan 10, 2012 4:23 pm
- Location: Vietnam
Re: 727 Equation TLE
Thanks
Re: 727 Equation TLE
will someone look into my issue...?
Re: 727 Equation TLE
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.gr81 wrote:getting RE, please find the code at http://ideone.com/yvuQZE
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/-