plz help me with my code!!!

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int t;char s[20];
scanf("%d",&t);
while(t–)
{
gets(s);//fflush(stdin);
//scanf("%s[^/n]",&s);
if((s[0]>=‘a’&&s[0]<=‘h’)&&(s[1]>=‘1’&&s[1]<=‘8’)&&(s[2]==’-’)&&(s[3]>=‘a’&&s[3]<=‘h’)&&(s[4]>=‘1’&&s[4]<=‘8’))
{
if((abs(s[0]-s[3])==2)&&(abs(s[1]-s[4])==1)||(abs(s[0]-s[3])==1)&&(abs(s[1]-s[4])==2))
printf(“Yes\n”);
else
printf(“No\n”);
}
else
printf(“Error\n”);
}
return 0;
}

the compiler is giving wrong answer…plz hlp me find it

1 Like

@sagar1pant7 : Please give problem code or link to problem page . Also give a link to your submission , as the code pasted here is not easily readable . That will help others to help you better .

Hello @sagar1pant7,

Compare this solution with your own and see the differences… :wink:

At 1st sight, I’d say that the incorrect part of your code is on the Knight Move itself…

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	int TST;
	scanf("%d",&TST);
	getchar();
	for(int tst=0;tst<TST;tst++)
	{
		char s[111];
		gets(s);
		if(strlen(s)==5 && 
			'a'<=s[0] && s[0]<='h' &&
			'1'<=s[1] && s[1]<='8' &&
			s[2]=='-' &&
			'a'<=s[3] && s[3]<='h' &&
			'1'<=s[4] && s[4]<='8')
		{
			if(abs((s[0]-s[3])*(s[1]-s[4]))==2)
				puts("Yes");
			else
				puts("No");
		}
		else puts("Error");
	}
	return 0;
}

The above code gets AC (it’s setter’s solution, which just seems to be a “cleaner” implementation of the same idea you had)!

Hope you can fix it,

Bruno

PS: @vineetpaliwal, Sagar actually tagged the post, so you can get a reference for the problem editorial and respective links in contest and practice page on the right side of the page :slight_smile:

1 Like

@sagar1pant7 : There is minor “input reading” related problem in your code .
Replace the following line :
scanf("%d",&t);
with :
scanf("%d\n",&t);
and then it gives correct answer .
The problem is if new line character is not read , then the first time you read a candidate chess move from input you are actually reading the newline character and analyzing it .

1 Like