POJ-1328(c++)

20150201

题目:

一个无限长的直线海岸,要在海岸上建造雷达监察海上小岛.如何建,使用最少的雷达.

输入:

小岛数  雷达监察半径

小岛坐标x 小岛坐标y

输出:

最小雷达数

解题思路:

以小岛为中心画圆,与海岸的相交区间.

代码:

#include <iostream>
#include <cmath>

using namespace std;

#define MAX 1000

int islandX[MAX];
int islandY[MAX];
int cases = 0;

int main()
{
    int numOfIsland,radium;
    while(cin >> numOfIsland >> radium){
        int counts = 0;
        bool flag = false;
        //中止条件
        if(!numOfIsland&&!radium)
            break;
        //输入
        int tempX,tempY;
        for(int i = 0; i < numOfIsland; i++){
            cin >> tempX >> tempY;
            if(tempY>radium)
                flag = true;
            //插入排序
            int j;
            for(j = i-1; j >= 0; j--){
                if(islandX[j]>tempX){
                    islandX[j+1] = islandX[j];
                    islandY[j+1] = islandY[j];
                }else{
                    break;
                }
            }
            islandX[j+1] = tempX;
            islandY[j+1] = tempY;
        }
        //不可解条件
        if(flag){
            cout << "Case " << ++cases << ": " << -1 << endl;
            continue;
        }
        //小岛数为0时
        if(!numOfIsland){
            cout << "Case " << ++cases << ": " << 0 << endl;
            continue;
        }
        //解法 以小岛为中心画圆
        counts = 1;
        double left[MAX];
        double right[MAX];
        double temp = islandX[0] + sqrt(radium*radium-islandY[0]*islandY[0]);
        for(int i = 1; i < numOfIsland; i++){
            left[i] = islandX[i] - sqrt(radium*radium-islandY[i]*islandY[i]);
            right[i] = islandX[i] + sqrt(radium*radium-islandY[i]*islandY[i]);
            if(left[i] > temp){
                temp = right[i];
                counts++;
            }else if(right[i] < temp){
                temp = right[i];
            }
        }
        cout << "Case " << ++cases << ": " << counts << endl;
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值