首页 > 试题广场 >

炮台攻击

[编程题]炮台攻击
  • 热度指数:22960 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
兰博教训提莫之后,然后和提莫讨论起约德尔人,谈起约德尔人,自然少不了一个人,那 就是黑默丁格------约德尔人历史上最伟大的科学家. 提莫说,黑默丁格最近在思考一个问题:黑默丁格有三个炮台,炮台能攻击到距离它小于等于R的敌人 (两点之间的距离为两点之间直线距离,例如(3,0),(0,4)之间的距离是5),如果一个炮台能攻击 到敌人,那么就会对敌人造成1×的伤害.黑默丁格将三个炮台放在N*M方格中的点上,并且给出敌人 的坐标. 问:那么敌人受到伤害会是多大?

输入描述:
每一行输入9个整数R x1 y1 x2 y2 x3 y3 x0 y0
其中R代表炮台攻击的最大距离,(x1,y1),(x2,y2),(x3,y3)代表三个炮台的坐标,(x0,y0)代表敌人的坐标。


输出描述:
输出一行,这一行代表敌人承受的最大伤害,(如果每个炮台都不能攻击到敌人,输出0×)
示例1

输入

1 1 1 2 2 3 3 1 2

输出

2x
推荐
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
inline int sqr(int x){return x * x;}
int main(){
	int R,x[6],y[6];
	while(scanf("%d",&R) != EOF){
		for(int i = 0;i < 4;++ i) scanf("%d%d",&x[i],&y[i]);
		int ret = 0;
		for(int i = 0;i < 3;++ i)
		if(sqr(x[i] - x[3]) + sqr(y[i] - y[3]) <= sqr(R))
			++ ret;
		printf("%dx\n",ret);
	}
	return 0;
}
编辑于 2016-03-01 14:24:14 回复(11)
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int R = scanner.nextInt();
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            int x3 = scanner.nextInt();
            int y3 = scanner.nextInt();
            int x0 = scanner.nextInt();
            int y0 = scanner.nextInt();
            int count = 0;
            if (Math.sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0)) < R) {
                count++;
            }
            if (Math.sqrt((x2 - x0)*(x2 - x0) + (y2 - y0)*(y2 - y0)) < R) {
                count++;
            }
            if (Math.sqrt((x3 - x0)*(x3 - x0) + (y3 - y0)*(y3 - y0)) < R) {
                count++;
            }
            System.out.println(count + "x");
        }
    }
}

发表于 2024-06-24 16:04:10 回复(0)
很简单,直接计算三个炮台与敌人之间的间距,如果距离在炮台的射程之内就增加伤害。唯一需要注意的是本题有多组用例,记得循环读入用例。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while((line = br.readLine()) != null){
            String[] params = line.split(" ");
            int R = Integer.parseInt(params[0]);
            int x1 = Integer.parseInt(params[1]);
            int y1 = Integer.parseInt(params[2]);
            int x2 = Integer.parseInt(params[3]);
            int y2 = Integer.parseInt(params[4]);
            int x3 = Integer.parseInt(params[5]);
            int y3 = Integer.parseInt(params[6]);
            int x0 = Integer.parseInt(params[7]);
            int y0 = Integer.parseInt(params[8]);
            int count = 0;
            if((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0) <= R*R){
                count++;
            }
            if((x2 - x0)*(x2 - x0) + (y2 - y0)*(y2 - y0) <= R*R){
                count++;
            }
            if((x3 - x0)*(x3 - x0) + (y3 - y0)*(y3 - y0) <= R*R){
                count++;
            }
            System.out.println(count + "x");
        }
    }
}

发表于 2022-01-13 13:00:47 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            int R = sc.nextInt();
            Point[] nums = new Point[4];
            int i = -1;
            while(i++ < 3 && sc.hasNext()){
                int x = sc.nextInt();
                int y = sc.nextInt();
                nums[i] = new Point(x , y);
            }
            int farm = 0;
            for (int j = 0; j < 3; j++) {
                if(nums[3].distance(nums[j]) <= R) farm++;
            }
            System.out.println(farm + "x");
        }
    }

    static class Point{
        private int x;
        private int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public double distance(Point point){
            double distance = (x - point.x) * (x - point.x) + (y - point.y) * (y - point.y);
            return Math.pow(distance , 0.5);
        }
    }
}

发表于 2021-08-20 21:17:27 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int R = sc.nextInt();
            int[] tab1 = new int[2];
            int[] tab2 = new int[2];
            int[] tab3 = new int[2];
            int[] tab = new int[2];
            tab1[0] = sc.nextInt();
            tab1[1] = sc.nextInt();
            tab2[0] = sc.nextInt();
            tab2[1] = sc.nextInt();
            tab3[0] = sc.nextInt();
            tab3[1] = sc.nextInt();
            tab[0] = sc.nextInt();
            tab[1] = sc.nextInt();
            int res = 0;
            res += solve(R, tab1, tab);
            res += solve(R, tab2, tab);
            res += solve(R, tab3, tab);
            System.out.println(res + "x");
        }
    }
    private static int solve(int R, int[] tab1, int[] tab){
        double dis = Math.sqrt(Math.pow(tab1[0]-tab[0],2) + Math.pow(tab1[1]-tab[1],2));
        return dis > R ? 0 : 1;
    }
}

发表于 2020-08-15 14:42:24 回复(0)
import java.util.Scanner;

public class Main {
	private static class Point{
		private int x;
		private int y;
		
		public Point(int x, int y) {
			// TODO Auto-generated constructor stub
			this.x = x;
			this.y = y;
		}
		
		public double getDistance(Point point){
			int dd = (x - point.x) * (x - point.x) + (y - point.y) * (y - point.y);
			return Math.pow(dd, 0.5);
		}
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int r = in.nextInt();
			Point[] b = new Point[4];
			int i = -1;
			while (++i<4 && in.hasNext()) {
				int x = in.nextInt();
				int y = in.nextInt();
				b[i] = new Point(x, y);
			}
			
			int harm = 0;
			for(int j=0; j<3; j++){
				if (b[3].getDistance(b[j])<=r) {
					harm++;
				}
			}
			
			System.out.println(harm+"x");
		}
	}
}


发表于 2017-08-01 15:47:44 回复(0)
//到三个炮台分别到敌人的距离即可
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String[] s= sc.nextLine().split(" ");
            int R = Integer.parseInt(s[0]);
            int[] x = new int[3];
            int[] y = new int[3];
            x[0] = Integer.parseInt(s[1]);
            y[0] = Integer.parseInt(s[2]);
            x[1] = Integer.parseInt(s[3]);
            y[1] = Integer.parseInt(s[4]);
            x[2] = Integer.parseInt(s[5]);
            y[2] = Integer.parseInt(s[6]);
            int x0 = Integer.parseInt(s[7]);
            int y0 = Integer.parseInt(s[8]);
            int count= 0;
            for(int i=0;i<3;i++){//循环3次,分别算三个距离
                if(Math.sqrt(Math.pow(x[i]-x0,2) + Math.pow(y[i]-y0,2)) <= R){
                    count++;
                }
            }
            System.out.println(count+"x");
        }
    }
}

发表于 2017-03-09 16:57:47 回复(0)
//很朴素的思想,大家都能看懂的答案,通过了测试
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
int count = 0;
int R = scanner.nextInt();
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
int x3 = scanner.nextInt();
int y3 = scanner.nextInt();
int x0 = scanner.nextInt();
int y0 = scanner.nextInt();
if (attack(x0, y0, R, x1, y1))
count++;
if (attack(x0, y0, R, x2, y2))
count++;
if (attack(x0, y0, R, x3, y3))
count++;

System.out.println(count + "x");
}

public static boolean attack(int x0, int y0, int R, int x, int y) {
return  Math.pow(x0 - x, 2) + Math.pow(y0 - y, 2) <= Math.pow(R, 2);
}
}
发表于 2017-02-25 09:56:33 回复(0)