14 / 26
Aug 2012

here is my code sir......

#include<stdio.h>
int main()
{
    int nt=0,i=0,n=0,m=0,j=0,x1=0,x2=0,y1=0,y2=0,xt=0,yt=0;
    scanf("%d",&nt);
    for(i=0;i<nt;i++)
    {
                     scanf("%d %d",&n,&m);
                     int x[n],y[n];int out[m],k=0,l=0;
                     for(j=0;j<n;j++)
                     {
                                     scanf("%d %d",&x[j],&y[j]);
                                     }
                     for(j=0;j<m;j++)
                     {
                                     scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                                     int lhs=0,rhs=0;
                                     lhs=(xt-x1)*(y2-y1);
                                     rhs=(yt-y1)*(x2-x1);
                                     int cnt=0;
                                     for(l=0;l<n;l++)
                                     {
                                                     if(x[l]>=x1 && y[l]>=y1 && x[l]<=x2 && y[l]<=y2)
                                                     {
                                                     if(((x[l]-x1)*(y2-y1))==((y[l]-y1)*(x2-x1)))
                                                                 cnt++;
                                                                 }
                                                                 }
                                     out[j]=cnt;
                                     }
                     for(j=0;j<m;j++)
                     {
                                     printf("%d\n",out[j]);
                                     }
                                    }
          return 0;
          }

Your issue lies mainly in this line of code:
if(x[l]>=x1 && y[l]>=y1 && x[l]<=x2 && y[l]<=y2)

1 month later

sir i am getting correct aswer for test case and the case given in forum but WA when i submit it at spoj.
dont know why.
[

Your issue is almost 100% identical to the poster above...
if(a[j]>=x1[i]&&a[j]<=x2[i])

if((a[j]>=x1[i]&&a[j]<=x2[i])||(a[j]<=x1[i]&&a[j]>=x2[i]))

this is also giving wrong answer

but why?
coordinates are positive only not negative so i shouold not consider the negative case

ok i read it again .
coordinated of segment can be -ve bt of points are +ve.i am right?
i changed code but getting wrong only.

if 2nd x coordinate is -ve i have interchanged both points of segment

It's not a matter of positive or negative. If all of the variables in this if statement are positive it still has the same issue.

u want to say that 5 5 0 0 can be input so

if(1>=5 && 1<=0)

will be false.
i have considered this case also in below code.

Your algorithm is not accurate. You have not considered all possibilities.

kkkkk......i wil code with a new approach but.....i just want 2 know what is wrong with this approach......inspite it is lengthy i want 2 know where do i fail......???

The biggest issue is that you are assuming that the slope will be an integer value. As mentioned before you should abandon this solution and simplify it greatly. Work with a pen and paper for a while and find a better algorithm.

i dont have patience to read this much length of code like leppy sir wink wink
but what i caught in 3 seconds was

m1=(points[j][1]-start[i][1])/(points[j][0]-start[i][0]);

if points[j][0]=0 and start[i][0]=0;
x/0 underministic
and rest follow what sir say

@Leppy-----i worked on some more furious test cases and debugged my code .............CODE REMOVED

Please fix your indentation. Please explain your algorithm. Please explain why you think this algorithm is accurate.

1 month later

I have some trouble with that problem though it looks like an easy one. I'm not sure if it's a matter of wrong logic, I/O problem (with Python) or just misunderstanding the problem description. "Segment" is a rectangle, right?

For this testcase from a post above:

1
4 5
0 3
2 2
3 3
4 4
0 0 5 5
3 3 4 4
0 0 1 0
0 0 0 1
0 0 0 5

The output should be:

4
2
0
0
1

Right?

Thanks a lot, leppy! Neither my printed dictionary nor the google translator did offer the correct meaning.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 40 Jun 29

Want to read more? Browse other topics in C and C++ or view latest topics.