要用 Java 实现这个任务,可以先定义一个类来表示炸雷和排雷火箭,每个炸雷或排雷火箭由坐标 (x,y) 和半径 r 组成。可以这样定义这个类:
class Explosion {
int x;
int y;
int r;
public Explosion(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
}
然后可以写一个函数来判断两个炸雷或排雷火箭是否能互相爆炸。可以这样实现:
boolean canExplode(Explosion e1, Explosion e2) {int dx = e1.x - e2.x;
int dy = e1.y - e2.y;
double distance = Math.sqrt(dx * dx + dy * dy);
return distance <= e1.r + e2.r;
}
最后,可以读入输入数据,并使用上述函数来统计答案。代码如下:
``` import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); Explosion[] mines = new Explosion[n]; for (int i = 0; i < n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); int r = scanner.nextInt(); mines[i] = new Explosion(x, y, r); } Explosion[] rockets = new Explosion[m]; for (int i = 0; i < m; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); int r = scanner.nextInt(); rockets[i] = new Explosion(x, y, r); } int count = 0; for (Explosion mine : mines) { for (Explosion rocket : rockets) { if (canExplode(mine, rocket)) { count++; break; } } } System.out.println(count); } }
class Explosion { int x; int y; int r;
publicExplosion(int x, int y, int r) {