POJ 1328 雷达安装(贪心)

先计算出以每个小岛为圆心,算出每个小岛与海岸的左右交点。
之后进行比较,有以下三种情况:在这里插入图片描述

WA了很多次,最后终于AC的代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct Land
{
	int x;
	int y;
	double x_r;
	double x_l;
 } land[1100];
 void sort(struct Land land[],int n){
	int i,j;
	struct Land temp; 
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-1-i;j++)
		{
			if(land[j].x_l>land[j+1].x_l)
			{
				temp=land[j];
				land[j]=land[j+1];
				land[j+1]=temp;
			}
		}
	 } 
 }
int main()
{
	int i,j,n,r,k=1,outcome=1;
	while(1)
	{
		scanf("%d %d",&n,&r);
		if(n==0&&r==0)
			break;
		for(i=0;i<n;i++)
		{
			scanf("%d %d",&land[i].x,&land[i].y);
			if(land[i].y<0||land[i].y>r)
			{
				outcome=-1;
				//不能加break 加了就会WA
			}
			land[i].x_l=land[i].x-sqrt((double)r*r-land[i].y*land[i].y);
			land[i].x_r=land[i].x+sqrt((double)r*r-land[i].y*land[i].y);
		}
		if(outcome==-1)
		{
			printf("Case %d: -1\n",k);
			k++;
			outcome=1;//在输出结束之后也不能忘记改变k和outcome的值
			continue;
		}
		sort(land,n);
		for(i=1,j=0;i<n;i++)
		{
			if(land[j].x_r>land[i].x_r)
				j=i;
			if(land[j].x_r<land[i].x_l)
			{
				outcome++;
				j=i;
			}
		}
		printf("Case %d: %d\n",k,outcome);
		k++;
		outcome=1;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值