Java实现蓝桥杯VIP算法训练 小生物的逃逸

试题 算法训练 小生物的逃逸

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  空间中有n个球,这些球不相交也不相切。有m个可以视为质点的小生物,可能在某些球内,也可能在所有球之外,但不会在球面上。问这些生物从原来的地方逃逸到所有球外面的空间,至少要经过多少层球面。
输入格式
  第一行两个数n、m:表示球的数量和小生物的数量;
  接下来n行每行四个整数Xi、Yi、Zi和Ri:表示一个球的三维坐标和半径;
  接下来m行每行三个整数Xi、Yi、Zi:表示一个生物的坐标。
输出格式
  一行m个数:表示每个小生物逃逸时至少经过的球面数。
样例输入
2 2
0 0 0 2
0 0 0 4
0 0 1
0 0 3
样例输出
2 1
数据规模和约定
  1<=n、m<=100,|Xi|、|Yi|、|Zi|<=10000,1<=Ri<=10000;
  数据保证所有球严格不接触,小生物都不在球面上。

package 第十次模拟;

import java.util.Scanner;

public class 小生物的逃逸 {
	  public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int n = sc.nextInt();
	        int m = sc.nextInt();
	        int[][] a = new int[n][4];
	        int[][] b = new int[m][3];
	        for (int i = 0; i < n + m; i++) {
	            if (i < n) {
	                for (int j = 0; j < 4; j++) {
	                    a[i][j] = sc.nextInt();
	                }
	            } else {
	                for (int j = 0; j < 3; j++) {
	                    b[i - n][j] = sc.nextInt();
	                }
	            }
	        }
	        for (int i = 0; i < m; i++) {
	            int count = 0;
	            for (int j = 0; j < n; j++) {
	                int k = 2, sum = 0;
	                do {
	                    sum += (b[i][k] - a[j][k]) * (b[i][k] - a[j][k]);
	                } while ((k--) > 0);
	                if (sum < a[j][3] * a[j][3]) {
	                    count++;
	                }
	            }
	            System.out.print(count + " ");
	        }
	    }

}

试题 算法训练 小生物的逃逸

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  空间中有n个球,这些球不相交也不相切。有m个可以视为质点的小生物,可能在某些球内,也可能在所有球之外,但不会在球面上。问这些生物从原来的地方逃逸到所有球外面的空间,至少要经过多少层球面。
输入格式
  第一行两个数n、m:表示球的数量和小生物的数量;
  接下来n行每行四个整数Xi、Yi、Zi和Ri:表示一个球的三维坐标和半径;
  接下来m行每行三个整数Xi、Yi、Zi:表示一个生物的坐标。
输出格式
  一行m个数:表示每个小生物逃逸时至少经过的球面数。
样例输入
2 2
0 0 0 2
0 0 0 4
0 0 1
0 0 3
样例输出
2 1
数据规模和约定
  1<=n、m<=100,|Xi|、|Yi|、|Zi|<=10000,1<=Ri<=10000;
  数据保证所有球严格不接触,小生物都不在球面上。

package 第十次模拟;

import java.util.Scanner;

public class 小生物的逃逸 {
	  public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        int n = sc.nextInt();
	        int m = sc.nextInt();
	        int[][] a = new int[n][4];
	        int[][] b = new int[m][3];
	        for (int i = 0; i < n + m; i++) {
	            if (i < n) {
	                for (int j = 0; j < 4; j++) {
	                    a[i][j] = sc.nextInt();
	                }
	            } else {
	                for (int j = 0; j < 3; j++) {
	                    b[i - n][j] = sc.nextInt();
	                }
	            }
	        }
	        for (int i = 0; i < m; i++) {
	            int count = 0;
	            for (int j = 0; j < n; j++) {
	                int k = 2, sum = 0;
	                do {
	                    sum += (b[i][k] - a[j][k]) * (b[i][k] - a[j][k]);
	                } while ((k--) > 0);
	                if (sum < a[j][3] * a[j][3]) {
	                    count++;
	                }
	            }
	            System.out.print(count + " ");
	        }
	    }

}