## 727 - Equation

Moderator: Board moderators

IRA
Learning poster
Posts: 82
Joined: Sat Jan 07, 2006 6:52 am
I also got WA in this problem.

Kallol
Learning poster
Posts: 100
Joined: Sun Nov 13, 2005 8:56 am
I have absolutely bo idea why I am getting WA !!!

I have read all the posts here and my program passed all the input cases. But I am gettin WA again and again !!
Here i have no option but to seek ur help with my code. Why I am getting wrong?? Do you have any critical input that yields wrong output by my code. Plz help !!

Code: Select all

``````#include<stdio.h>
#include<string.h>
#include<ctype.h>

char s;

int next(int i)
{
while(s[i]!=')')
{
i++;
}

i--;
return i;
}

char convert(char c)
{
if(c=='+')
{
return 'p';
}
else if(c=='-')
{
return 's';
}
else if(c=='*')
{
return 'm';
}
else if(c=='/')
{
return 'd';
}
else if(c=='p')
{
return '+';
}
else if(c=='s')
{
return '-';
}
else if(c=='m')
{
return '*';
}
else if(c=='d')
{
return '/';
}
else
{
return 0;
}
}
void push(int pos,int c)
{
int i=strlen(s)+1;
while(i>pos)
{
s[i]=s[i-1];
i--;
}
s[pos]=c;
return;
}
void find_back_n_push(int pos,int c)
{
int i=pos-1,sum=0;
while(i)
{
if(s[i]==')')
{
sum++;
}
else if(s[i]=='(')
{
sum--;
}
if(sum==0)
{
break;
}
i--;
}
push(i,'(');

return;
}
void find_front_n_push(int pos,int c)
{
int i=pos+1,sum=0;
while(s[i])
{
if(s[i]==')')
{
sum++;
}
else if(s[i]=='(')
{
sum--;
}
if(sum==0)
{
break;
}
i++;
}
push(i+1,convert(c));
push(i+2,')');

return;
}
int Evaluate(int b,int l)
{
int i,r=0,rsum=0;
for(i=b;i<=l;i+=(r+1))
{
r=0;
if(s[i]=='(')
{
r=Evaluate(i+1,next(i));
}
rsum+=r;
l+=r;
}
r=0;
for(i=b;i<=l;)
{
if(s[i]=='*' || s[i]=='/')
{
char c=s[i];
find_back_n_push(i,c);

find_front_n_push(i+1,c);
i+=1;
s[i]=' ';
r+=3;
rsum+=3;
l+=3;
}
i++;
}
for(i=b;i<=l;)
{
if(s[i]=='+' || s[i]=='-')
{
char c=s[i];
find_back_n_push(i,c);
find_front_n_push(i+1,c);
i+=1;
s[i]=' ';
r+=3;
rsum+=3;
l+=3;
}
i++;
}
return rsum;
}
void print()
{
int i=0;
while(s[i])
{
if(s[i]>='0' && s[i]<='9')
{
printf("%c",s[i]);
}
if(isalpha(s[i]))
{
printf("%c",convert(s[i]));
}
i++;
}
printf("\n");
return;
}
int main(void)
{
char temp;
int test;
scanf("%d",&test);
gets(temp);
gets(temp);

while(test--)
{
strcpy(s,"");
while(1)
{
if(!gets(temp))
{
break;
}
if(strcmp(temp,"")==0)
{
break;
}
strcat(s,temp);
}
int l=strlen(s);
//	s[l]='\0';
int vua=Evaluate(0,l-1);
print();
if(test)
{
printf("\n");
}

}
return 0;
}
``````
Waiting for help ...
Syed Ishtiaque Ahmed Kallol
CSE,BUET

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm

### 727 - TLE

I am getting TLE in this problem. Could someone plz take at look at my code. Here it is:

//CODE REMOVED AFTER AC

Thanx in advance Last edited by unaben on Mon Jul 31, 2006 5:06 pm, edited 1 time in total.

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm
I got AC. Silly mistake sml
New poster
Posts: 15
Joined: Mon Jul 24, 2006 3:34 pm
Delete yer code.  And congrats.

Donotalo
Learning poster
Posts: 56
Joined: Sat Aug 20, 2005 11:05 am
i've tried all the input/output set in the forum and all seem work well. still getting WA. can anyone kindly check my i/o set? thanks in advance.

INPUT

Code: Select all

``````11

3
+
4
*
2

3
*
4
+
2

(
3
+
4
)
*
2

3
*
(
4
+
2
)

(
3
+
4
)
*
(
4
+
5
)

3
+
4
-
5

(
3
+
2
)
*
5

(
5
+
(
5
+
5
)
)

(
1
)

(
1
/
2
*
3
+
4
)
5

(
(
2
)
)
``````
OUTPUT

Code: Select all

``````342*+

34*2+

34+2*

342+*

34+45+*

34+5-

32+5*

555++

1

12/3*4+5*

2
`````` Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
My accepted code returns the same output. Try the following test case

Input:

Code: Select all

``````1

(
3
+
(
3
+
2
)
*
(
4
+
5
+
2
)
*
(
1
+
2
-
3
/
(
1
-
2
*
(
4
+
4
)
)
)
)
-
3
*
5``````
Output:

Code: Select all

``332+45+2+*12+31244+*-/-*+35*-``
Hope it helps.
Ami ekhono shopno dekhi...
HomePage

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

Code: Select all

``````CODE ACCEPTED
``````
I am getting a RTE. Please tell me what the problem could be.
Last edited by abhiramn on Sun Jun 03, 2007 6:50 am, edited 1 time in total.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
I just used same input twice and your code failed. Check the cases and I/O format.

Input:

Code: Select all

``````2

(
3
+
2
)
*
5

(
3
+
2
)
*
5``````
Output:

Code: Select all

``````32+5*

32+5*``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

### @jan

I did try those two inputs, but my code gives the same correct result. I did however change the code a little bit. And here it is. Same problem once again, RTE Code: Select all

``````CODE WAS ACCEPTED
``````
Last edited by abhiramn on Sun Jun 03, 2007 6:49 am, edited 1 time in total.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
I did run your code and I got segmentation fault which is RTE for sample input..
I guess taking input is wrong..

abhiramn
New poster
Posts: 29
Joined: Sat May 26, 2007 7:54 pm

### Thank you guys

I got AC. The problem was in this condition.....

if (ch=='\n')
.....break;

I changed it to

if(ch=='\n'||ch==EOF)
.....break;    Thanks a lot.

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm
Contact:
Hi,

727 gives many times wrong ans to me. I have searched all the topics of 727 in the board and match all the input/output. All the i/o match correctly. Please help me to locate my errors.

Code: Select all

``````
Code has been cut after a.c.
The problem is one the line
gets(input);
I have cut this line and got accepted. Thanks Jan vai for his help.
``````
Thanks all.
ABDULLAH.
Last edited by abdullah<cse du> on Fri Jul 13, 2007 5:43 pm, edited 1 time in total.
We were in past, we are in past and we will go in past.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
Try the cases.

Input:

Code: Select all

``````3

4
+
4
*
2

5
-
1
/
2

6
/
2``````
Output:

Code: Select all

``````442*+

512/-

62/``````
Hope these help.
Ami ekhono shopno dekhi...
HomePage

abdullah<cse du>
New poster
Posts: 39
Joined: Mon Dec 04, 2006 2:18 pm