首页 > 试题广场 >

炮台攻击

[编程题]炮台攻击
  • 热度指数: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)
importjava.util.*;
publicclassMain{
    publicstaticvoidmain(String[] args) {
        Scanner sc = newScanner(System.in);
        while(sc.hasNext()) {
            intr = sc.nextInt();
            intcount = 0;
            int[] x = newint[4];
            int[] y = newint[4];
            for(inti = 0; i < 4; i++) {
                x[i] = sc.nextInt();
                y[i] = sc.nextInt();
            }
            for(inti = 0; i < 3; i++) {
                if((x[i] - x[3])*(x[i] - x[3]) + (y[i] - y[3]) * (y[i] - y[3]) <= r * r) {
                    count++;
                }
            }
            System.out.println(count + "x");
        }
    }
}

发表于 2016-08-01 12:19:46 回复(0)
系统的测试用例会有很多组,需要用while(in.hasNext())循环不断接收数据,并且每组算完要将hurt重置为0,我刚开始也是只算了一组,导致下一组他输出不了东西
发表于 2016-03-21 14:41:19 回复(2)
 #include<iostream>
 #include<cmath> 
using namespace std;
 int main(void){ 
	 int a[8]; 
	 int R,i; 
	 double d1,d; 
	 while(cin>>R){
		int count =0;
		for(i=0;i<8;i++){ 
			 int x; 
			 cin>>x; 
			 a[i]=x; 
		 } 
		for(i=0;i<5;i=i+2){ 
			d1=(pow((a[i]-a[6]),2) + pow((a[i+1]-a[7]),2)); 
			d=sqrt(d1); 
			if(d <= R) 
				count+=1; 
		} 
		cout<<count<<"x"<<endl; 
	} 
	 return 0; 
 } 
通过所有测试用例

发表于 2016-03-22 16:10:45 回复(0)
#include<stdio.h>
#include<math.h>
#define DIS(x,y,xx,yy) (sqrt((xx-x)*(xx-x)+(yy-y)*(yy-y)))
int main(){
	int x1,y1,x2,y2,x3,y3,x0,y0;
	float R;
	while(~scanf("%f%d%d%d%d%d%d%d%d",&R,&x1,&y1,&x2,&y2,&x3,&y3,&x0,&y0)){
		int sum=0;
		if(DIS(x1,y1,x0,y0)<=R) sum++;
		if(DIS(x2,y2,x0,y0)<=R) sum++;
		if(DIS(x3,y3,x0,y0)<=R) sum++;
		printf("%dx\n",sum);
	}
	return 0;
}

发表于 2016-03-03 11:09:56 回复(0)
import java.util.Scanner;
import java.lang.Math;


public class Main {
	static int hurt = 0;
	
	public static void main(String[] args) {
		
		
		System.out.println("请输入攻击距离R和三个炮台的坐标x1, y1, x2, y2, x3, y3,
敌人的坐标x0, y0");
		Scanner in = new Scanner(System.in);
		
		double R = in.nextInt();
		double x1 = in.nextInt();
		double y1 = in.nextInt();
		double x2 = in.nextInt();
		double y2 = in.nextInt();
		double x3 = in.nextInt();
		double y3 = in.nextInt();
        double x0 = in.nextInt();
		double y0 = in.nextInt();
		
		
		injure(x1, y1, x0, y0, R);
		injure(x2, y2, x0, y0, R);
		injure(x3, y3, x0, y0, R);
		
		System.out.println("总共受到的伤害是:" + hurt + "X");
		
	}
	
	public static int injure(double x, double y, double x0, double y0, double r) {
		if (Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)) <= r) {
			hurt++;
		}
		return hurt;
	}
	
}
大家好,这是我做的答案,在eclipse里面测试牛客网给的测试用例能通过,但是在线给的测试用例通不过,有同感的吗?
评论里找到原因了:测试用例会有很多组,这个只能算一组,要用while(in.hasNext())循环不断接收数据,这样的话每组算完要将hurt重置为0XX

编辑于 2016-03-24 16:29:14 回复(14)
import java.io.*;
import java.util.*;
//我是第一次在线练习笔试,看了大家的才知道不能随便取名。之前取名Test编译都通不过。
public class Main{ public static void main(String args[]){ Scanner reader = new Scanner(System.in); int R,x1,y1,x2,y2,x3,y3,x0,y0; //System.out.println("请输入R、x1,y1,x2,y2,x3,y3,x0,y0");
//不能随便输出东西,只能输出结果 while(reader.hasNext()){ int sum = 0;//每轮数据初始化 R = reader.nextInt(); x1 = reader.nextInt(); y1 = reader.nextInt(); x2 = reader.nextInt(); y2 = reader.nextInt(); x3 = reader.nextInt(); y3 = reader.nextInt(); x0 = reader.nextInt(); y0 = reader.nextInt(); if (getSum(x1,y1,x0,y0,R) == 1){ sum++; } if(getSum(x2,y2,x0,y0,R) == 1){ sum++; } if(getSum(x3,y3,x0,y0,R) ==1){ sum++; } System.out.println(sum+"x"); } } private static int getSum(int x,int y, int m,int n,int r){ if( ((m-x)*(m-x) +(n-y)*(n-y)) <= (r*r ) ){ return 1;//在范围内返回1 } else{ return 0;//范围外返回0 } } }

发表于 2016-03-18 13:31:26 回复(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)
def damage(R,x1,y1,x2,y2,x3,y3,x0,y0):
    count = 0
    if (x0-x1)**2 + (y0-y1)**2<=R**2:
        count = count + 1 
    if (x0-x2)**2 + (y0-y2)**2<=R**2:
        count = count + 1 
    if (x0-x3)**2 + (y0-y3)**2<=R**2:
        count = count + 1 
    print(str(count)+"x")

    
while True:
    try:
        R,x1,y1,x2,y2,x3,y3,x0,y0 = map(int,input().strip().split())
        damage(R,x1,y1,x2,y2,x3,y3,x0,y0)
    except:
        break

发表于 2021-09-25 12:22:07 回复(0)
import math
def get_distance(att, enemy, r):
    dis = math.sqrt((att[0] - enemy[0]) ** 2 + (att[1] - enemy[1]) ** 2)
    return 1 if dis <= r else 0
while True:
    try:
        R, x1, y1, x2, y2, x3, y3, x0, y0 = map(int, input().split())
        enemy = (x0, y0)
        atts = [(x1, y1), (x2, y2), (x3, y3)]
        res = 0
        for att in atts:
            res += get_distance(att, enemy, R)
        print('{}x'.format(str(int(res))))
    except:
        break

发表于 2020-08-19 16:13:11 回复(0)
不明说有多条测试数据,exx
发表于 2020-08-05 21:23:21 回复(0)
import java.util.*;
import java.math.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int R=sc.nextInt();
            int[] x=new int[3];
            int[] y=new int[3];
            x[0]=sc.nextInt();
            y[0]=sc.nextInt();
            x[1]=sc.nextInt();
            y[1]=sc.nextInt();
            x[2]=sc.nextInt();
            y[2]=sc.nextInt();
            int x0=sc.nextInt();
            int y0=sc.nextInt();
            int count=0;
            for(int i=0;i<3;i++){
                if((x[i]-x0)*(x[i]-x0)+(y[i]-y0)*(y[i]-y0)<=R*R){
                    count++;
                }
            }
            System.out.println(count+"x");
        }
    }
} 

发表于 2018-10-15 14:26:57 回复(0)
package com.special.first;

import java.util.Scanner;

/**
 * 网易02-炮台攻击
 * 这是考察欧几里得距离公式吗??
 *
 * Create by Special on 2018/3/11 16:12
 */
public class Pro063 {
    static int[] row = new int[4], col = new int[4];

    public static boolean isInRange(double R, int x1, int y1, int x2, int y2){
        return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) <= R;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            double R = input.nextInt();
            R *= R;
            for(int i = 0; i < 4; i++){
                row[i] = input.nextInt();
                col[i] = input.nextInt();
            }
            int count = 0;
            for(int i = 0; i < 3; i++){
                if(isInRange(R, row[i], col[i], row[3], col[3])){
                    count++;
                }
            }
            System.out.println(count + "x");
        }
    }
}
发表于 2018-03-11 16:24:48 回复(0)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int R,x1,y1,x2,y2,x3,y3,x0,y0;
    while(cin>>R>>x1>>y1>>x2>>y2>>x3>>y3>>x0>>y0)
    {
        int count =0;
        double R1=sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));
        if(R1<R)
            count++;
        double R2=sqrt((x2-x0)*(x2-x0)+(y2-y0)*(y2-y0));
        if(R2<R)
            count++;
        double R3=sqrt((x3-x0)*(x3-x0)+(y3-y0)*(y3-y0));
        if(R3<R)
            count++;
        cout<<count<<"x"<<endl;
    }
    return 0;
}

发表于 2017-06-30 19:00:15 回复(1)
//到三个炮台分别到敌人的距离即可
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)
// 不需要sqrt()函数,直接平方求距离即可
#include <iostream>

using namespace std;

int main(){
    int R, x1, y1, x2, y2, x3, y3, x0, y0;
    while(cin >> R >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x0 >> y0){
        int d12 = (x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0);
        int d22 = (x2 - x0)*(x2 - x0) + (y2 - y0)*(y2 - y0);
        int d32 = (x3 - x0)*(x3 - x0) + (y3 - y0)*(y3 - y0);
        
        int count = 0;
        if(d12 <= R*R)
            count ++;
        if(d22 <= R*R)
            count ++;
        if(d32 <= R*R)
            count ++;
        
        cout << count << "x" << endl;
    }
    
    return 0;
}

编辑于 2016-09-05 16:57:49 回复(0)
#include <iostream>
#include <math.h>
using namespace std;

int main()
    {
    
    int r;
    while(cin>>r)
    {
        int pao[3][2];
        for(int i=0;i<3;i++)
            cin>>pao[i][0]>>pao[i][1];
        
        int x0,y0;
        cin>>x0>>y0;
        
        int cc=0;
        for(int i=0;i<3;i++)
            {
            float dist = sqrt(float(pao[i][0]-x0)*(pao[i][0]-x0)+float(pao[i][1]-y0)*(pao[i][1]-y0));
            if(dist <= r) cc++;
        }
        cout<<cc<<"x"<<endl;
    }   
}

发表于 2016-08-18 18:00:32 回复(0)
//    直观题,没任何算法,只是初中数学
#include<iostream>
#include<cmath>

using namespace std;

int main(void)
    {
    int R, x1, y1, x2, y2, x3, y3, x0, y0;
    int hurt = 0;
    while ( cin >> R >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x0 >> y0 )
        {
        if ( R >= sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)) )
           ++hurt;
        if ( R >= sqrt((x2-x0)*(x2-x0)+(y2-y0)*(y2-y0)) )
           ++hurt;
        if ( R >= sqrt((x3-x0)*(x3-x0)+(y3-y0)*(y3-y0)) )
           ++hurt;
        cout << hurt << 'x' << endl;
        hurt = 0;
    }
    return 0;
}

编辑于 2016-08-16 22:09:52 回复(0)
#include <iostream>
#include <math.h>
using namespace std;

int check(int a, int b, int x, int y, int distant) {
	float dis;
	dis = sqrt((x-a)*(x-a) + (y-b)*(y-b));
    if(dis <= (float)distant) return 1;
    else return 0;
}

int main(){
	int Data[8];
	int i,R,max;
	
	while(cin>>R){
		max=0;
		for(i=0; i<8; i++)
			cin>>Data[i];

		for(i=0; i<=4; i+=2)
			max+=check(Data[i], Data[i+1], Data[6], Data[7], R);

		cout<<max<<"x"<<endl;
	}
    
    return 0;
}

编辑于 2016-08-02 23:11:23 回复(0)
#include<iostream>
#include<cmath>
#include<vector>
 
using namespace std;
 
intmain()
{
    intR,x1,y1,x2,y2,x3,y3,x0,y0;
    while(cin>>R>>x1>>y1>>x2>>y2>>x3>>y3>>x0>>y0)
    {
        doubledis;
        intcnt = 0;
        doubleR1 = sqrt(pow((x1-x0),2)+pow((y1-y0),2));
        doubleR2 = sqrt(pow((x2-x0),2)+pow((y2-y0),2));
        doubleR3 = sqrt(pow((x3-x0),2)+pow((y3-y0),2));
        vector<double> vr;
        vr.push_back(R1);
        vr.push_back(R2);
        vr.push_back(R3);
        for(inti = 0;i<vr.size();++i)
            if(vr[i]<=R)
                ++cnt;
        cout<<cnt<<'x'<<endl;
    }
 
    return0;
}

发表于 2016-08-01 16:34:30 回复(0)