是圆的问题呢(洛谷P1652题题解,Java语言描述)

前言

好久没写博客啦~~

前一段时间忙别的事,也给自己好好“放了个假”,准备开始新的旅程了!!!

那就先刷刷水题陶冶情操吧~~

题目要求

P1652题目链接
在这里插入图片描述
在这里插入图片描述

分析

看起来挺迷惑,实际上还挺简单的。

我们这么来思考这个问题:

两圆之间的位置关系有5种——内含外离相交内切外切
题目要求“不相交”,即不能有“相交”、“内切”、“外切”,所以只有“内含”、“外离”。

既然是任意曲线,且不能相交,那么我们一定可以有N条路径,可以与指定点不位于其中的任何圆不相交。(这句话好好读读,比较hhhhh)这样就可以求我们要求的“最少”。

据上分析,我们关注的焦点就是——指定点位于哪些圆内部

但是,我们还要注意,如果两个点同时位于同一个圆内,则可以不穿过这个圆

这样就有两种情况啦:

  1. 点1在,点2不在
  2. 点1不在,点2在

所以就可以写我们的算法啦。。。

所有数据读进去,用数组就可以随机访问,且索引一一对应啦!!

AC代码(Java语言描述)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int[] x_array = new int[num];
        int[] y_array = new int[num];
        int[] radius_array = new int[num];
        for (int i = 0; i < num; i++) {
            x_array[i] = scanner.nextInt();
        }
        for (int i = 0; i < num; i++) {
            y_array[i] = scanner.nextInt();
        }
        for (int i = 0; i < num; i++) {
            radius_array[i] = scanner.nextInt();
        }
        int x1 = scanner.nextInt(), y1 = scanner.nextInt(), x2 = scanner.nextInt(), y2 = scanner.nextInt();
        scanner.close();
        int counter = 0;
        for (int i = 0; i < num; i++) {
            if (Math.pow((x1 - x_array[i]), 2) + Math.pow(y1 - y_array[i], 2) <= Math.pow(radius_array[i], 2)) {
                if (!(Math.pow((x2 - x_array[i]), 2) + Math.pow(y2 - y_array[i], 2) <= Math.pow(radius_array[i], 2))) {
                    counter++;
                }
            } else {
                if (Math.pow((x2 - x_array[i]), 2) + Math.pow(y2 - y_array[i], 2) <= Math.pow(radius_array[i], 2)) {
                    counter++;
                }
            }
        }
        System.out.println(counter);
    }
}

祝大家新年快乐!!!

发布了351 篇原创文章 · 获赞 610 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43896318/article/details/104016342
今日推荐