727 - Equation
Moderator: Board moderators
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 !!
Waiting for help ...
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[50000];
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[10];
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;
}
Syed Ishtiaque Ahmed Kallol
CSE,BUET
Bangladesh
CSE,BUET
Bangladesh
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
OUTPUT
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
)
)
Code: Select all
342*+
34*2+
34+2*
342+*
34+45+*
34+5-
32+5*
555++
1
12/3*4+5*
2

My accepted code returns the same output. Try the following test case
Input:
Output:
Hope it helps.
Input:
Code: Select all
1
(
3
+
(
3
+
2
)
*
(
4
+
5
+
2
)
*
(
1
+
2
-
3
/
(
1
-
2
*
(
4
+
4
)
)
)
)
-
3
*
5
Code: Select all
332+45+2+*12+31244+*-/-*+35*-
Ami ekhono shopno dekhi...
HomePage
HomePage
Code: Select all
CODE ACCEPTED
Last edited by abhiramn on Sun Jun 03, 2007 6:50 am, edited 1 time in total.
I just used same input twice and your code failed. Check the cases and I/O format.
Input:
Output:
Hope these help.
Input:
Code: Select all
2
(
3
+
2
)
*
5
(
3
+
2
)
*
5
Code: Select all
32+5*
32+5*
Ami ekhono shopno dekhi...
HomePage
HomePage
@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
Thanks for your reply. Hoping you would help me out again.

Thanks for your reply. Hoping you would help me out again.
Code: Select all
CODE WAS ACCEPTED
Last edited by abhiramn on Sun Jun 03, 2007 6:49 am, edited 1 time in total.
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.
if (ch=='\n')
.....break;
I changed it to
if(ch=='\n'||ch==EOF)
.....break;




-
- New poster
- Posts: 39
- Joined: Mon Dec 04, 2006 2:18 pm
- Location: Bangladesh(CSE DU)
- 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.
Thanks all.
ABDULLAH.
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.
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.
Try the cases.
Input:
Output:
Hope these help.
Input:
Code: Select all
3
4
+
4
*
2
5
-
1
/
2
6
/
2
Code: Select all
442*+
512/-
62/
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- New poster
- Posts: 39
- Joined: Mon Dec 04, 2006 2:18 pm
- Location: Bangladesh(CSE DU)
- Contact: