C language poj1328 questions

The radius of the island is d, and the two intersections of the x-axis form a line segment. The radar that can scan to the island must be on the line segment, and all the line segments are sorted according to the size of the left endpoint. If the right endpoint of the former Island is larger than the left endpoint of the latter Island, the two islands can share a radar. Otherwise, the radar number + 1. If it can be shared, the next time as The criterion is the smaller island position of the right end of the first two islands that can be shared.
This question has been submitted on the ACM website of Peking University

# include<stdio.h>
# include<math.h> 
typedef struct
{
    double left;
    double right;
}island;

int main()
{
    island position[1000],temp,p;
    int n,d,count,order=0;                              // count how many islands, order the first case 
    int x,y,i,j,flag,min;                              //flag=1 output-1 
    double m;
    while(1)
    {  flag=0;
       count=0; 
       scanf("%d %d",&n,&d);
        if(n==0&&d==0)
           break;
        if(d<0||n<0)
           flag=1;
        for(i=0;i<n;i++)
            {
                scanf("%d %d",&x,&y);
                if(y<0||y>d)
                  flag=1;
                else
                {
                    m=d*d-y*y;
                    position[i].left=x-sqrt(m);
                    position[i].right=x+sqrt(m);        // The parameter of sqrt is double or float 
                }
            }
        order++;
        printf("Case %d: ",order);  
        if(flag==1)
        {
            printf("%d\n",-1);
            continue;
        }
        count=1;
        for(i=0;i<n;i++)                                              //Sort left to right 
            {
                min=i;
                for(j=i+1;j<n;j++)

                    if(position[min].left>position[j].left)
                       min=j;
                if(min!=i)
                       {
                        temp=position[i];
                        position[i]=position[min];
                        position[min]=temp;
                       } 
            }
        if(n==1&&y<=d)
            count=1;
        p=position[0];
        for(i=1;i<n;i++)                                        //  p last Island 
            {
                if(p.right<position[i].left)
                   {
                    count++;
                    p=position[i];
                   }
                else
                {
                    if(p.right>position[i].right)
                       p=position[i];
                }   
           }  

        printf("%d\n",count); 
    }
}

Posted by border20 on Mon, 30 Dec 2019 20:49:26 -0800