当前位置:实例文章 » Python实例» [文章]【2023】华为OD机试真题Java CC++ Python JS Go-题目0245-信号发射和接收

【2023】华为OD机试真题Java CC++ Python JS Go-题目0245-信号发射和接收

发布人:shili8 发布时间:2024-02-03 20:13 阅读次数:71

题目0245-信号发射和接收问题描述:
在一个信号发射和接收系统中,有多个信号发射器和接收器。每个信号发射器都有一个唯一的ID和一个信号发射范围。每个接收器也有一个唯一的ID和一个接收范围。当一个信号发射器的信号范围内有一个或多个接收器时,这些接收器都会接收到该信号。现在给定所有信号发射器和接收器的信息,请你计算有多少个接收器会接收到至少一个信号。

输入:
输入包含多组测试数据。每组数据第一行包含一个整数n(1≤n≤1000),表示信号发射器的数量。接下来n行,每行包含两个整数x和y(0≤x,y≤1000),分别表示信号发射器的ID和信号发射范围。接下来一行包含一个整数m(1≤m≤1000),表示接收器的数量。接下来m行,每行包含两个整数x和y(0≤x,y≤1000),分别表示接收器的ID和接收范围。输入保证所有ID都是唯一的。

输出:
对于每组测试数据,输出一个整数,表示至少有一个信号被接收到的接收器数量。

示例:
输入:
21223312233421223212233122334输出:
3223示例解释:
第一组数据中,信号发射器1的范围内有接收器1和2,信号发射器2的范围内有接收器2和3,所以一共有3个接收器接收到了信号。
第二组数据中,信号发射器1的范围内有接收器1和2,信号发射器2的范围内有接收器2和3,所以一共有2个接收器接收到了信号。

javaimport java.util.*;

public class Main {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 while (sc.hasNext()) {
 int n = sc.nextInt();
 int[][] transmitters = new int[n][2];
 for (int i =0; i < n; i++) {
 transmitters[i][0] = sc.nextInt();
 transmitters[i][1] = sc.nextInt();
 }
 int m = sc.nextInt();
 int[][] receivers = new int[m][2];
 for (int i =0; i < m; i++) {
 receivers[i][0] = sc.nextInt();
 receivers[i][1] = sc.nextInt();
 }
 int result = countReceivers(transmitters, receivers);
 System.out.println(result);
 }
 }

 public static int countReceivers(int[][] transmitters, int[][] receivers) {
 int count =0;
 for (int i =0; i < receivers.length; i++) {
 for (int j =0; j < transmitters.length; j++) {
 int distance = (receivers[i][0] - transmitters[j][0]) * (receivers[i][0] - transmitters[j][0]) +
 (receivers[i][1] - transmitters[j][1]) * (receivers[i][1] - transmitters[j][1]);
 if (distance <= transmitters[j][1] * transmitters[j][1]) {
 count++;
 break;
 }
 }
 }
 return count;
 }
}


def countReceivers(transmitters, receivers):
 count =0 for receiver in receivers:
 for transmitter in transmitters:
 distance = (receiver[0] - transmitter[0]) **2 + (receiver[1] - transmitter[1]) **2 if distance <= transmitter[1] **2:
 count +=1 break return countwhile True:
 try:
 n = int(input())
 transmitters = []
 for _ in range(n):
 transmitters.append(list(map(int, input().split())))
 m = int(input())
 receivers = []
 for _ in range(m):
 receivers.append(list(map(int, input().split())))
 result = countReceivers(transmitters, receivers)
 print(result)
 except:
 break


cpp#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int countReceivers(vector<vector<int>>& transmitters, vector<vector<int>>& receivers) {
 int count =0;
 for (auto receiver : receivers) {
 for (auto transmitter : transmitters) {
 int distance = pow(receiver[0] - transmitter[0],2) + pow(receiver[1] - transmitter[1],2);
 if (distance <= pow(transmitter[1],2)) {
 count++;
 break;
 }
 }
 }
 return count;
}

int main() {
 int n, m;
 while (cin >> n) {
 vector<vector<int>> transmitters(n, vector<int>(2));
 for (int i =0; i < n; i++) {
 cin >> transmitters[i][0] >> transmitters[i][1];
 }
 cin >> m;
 vector<vector<int>> receivers(m, vector<int>(2));
 for (int i =0; i < m; i++) {
 cin >> receivers[i][0] >> receivers[i][1];
 }
 int result = countReceivers(transmitters, receivers);
 cout << result << endl;
 }
 return0;
}


javascriptfunction countReceivers(transmitters, receivers) {
 let count =0;
 for (let receiver of receivers) {
 for (let transmitter of transmitters) {
 let distance = Math.pow(receiver[0] - transmitter[0],2) + Math.pow(receiver[1] - transmitter[1],2);
 if (distance <= Math.pow(transmitter[1],2)) {
 count++;
 break;
 }
 }
 }
 return count;
}

while (true) {
 let n = parseInt(readline());
 if (isNaN(n)) {
 break;
 }
 let transmitters = [];
 for (let i =0; i < n; i++) {
 transmitters.push(readline().split(' ').map(Number));
 }
 let m = parseInt(readline());
 let receivers = [];
 for (let i =0; i < m; i++) {
 receivers.push(readline().split(' ').map(Number));
 }
 let result = countReceivers(transmitters, receivers);
 print(result);
}


gopackage mainimport (
"fmt"
"math"
)

func countReceivers(transmitters [][]int, receivers [][]int) int {
	count :=0	for _, receiver := range receivers {
	for _, transmitter := range transmitters {
	distance := math.Pow(float64(receiver[0]-transmitter[0]),2) + math.Pow(float64(receiver[1]-transmitter[1]),2)
	if distance <= math.Pow(float64(transmitter[1]),2) {
	count++
	break}
}
}
	return count}

func main() {
	var n, m int	for {
_, err := fmt.Scan(&n)
	if err != nil {
	break}
	transmitters := make([][]int, n)
	for i :=0; i < n; i++ {
	transmitters[i] = make([]int,2)
	fmt.Scan(&transmitters[i][0], &transmitters[i][1])
}
	fmt.Scan(&m)
	receivers := make([][]int, m)
	for i :=0; i < m; i++ {
	receivers[i] = make([]int,2)
	fmt.Scan(&receivers[i][0], &receivers[i][1])
}
	result := countReceivers(transmitters, receivers)
	fmt.Println(result)
}
}

相关标签:c++javapythonjs
其他信息

其他资源

Top