C/C++ Programming Learning-Week 9 ② Rescue

Topic link

Title description

The lifeboat sets off from the base camp and rescues a number of people on the roofs back to the base camp. The number of roofs and the coordinates and number of people on each roof will be determined by the input, and the time it takes for everyone to reach the base camp and land.

The origin of the Cartesian coordinate system is the base camp. Every time the lifeboat sets off from the base camp, it will send people back to the base camp after saving people. The points in the coordinate system represent roofs, and each roof is represented by its position coordinates and the number of people on it.

Each time the lifeboat departs from the base camp, it sails to the next roof at a speed of 50 m/min. After reaching a roof, rescue everyone on it and board the boat for 1 minute each. Then the boat will return to the base camp and disembark for 0.5 minutes per person. Assume that the line connecting the origin to any roof does not pass through other roofs.

Input format The
first line, an integer, represents the number of roofs n (1≤n≤100).

Next, there are n lines of input. The first two real numbers in each line represent the plane coordinate position (xi, yi) of the roof relative to the base camp (in meters), and then an integer ri representing the number of people, separated by a space. 0 ≤ xi, yi ≤ 100, 1 ≤ ri ≤100.

Output format
One line, total time required for rescue, accurate to the minute (rounded up).

Sample Input

1
30 40 3

Sample Output

7

Ideas

The meaning of the question is to rescue one place at a time, give the rescue coordinates, and the number of people who need to be on the ship each time. It takes time to board and disembark. It also takes time to go in and out of the ship. Let's calculate the total cost. Time, rounded up to the nearest minute.

In other words, we only need to calculate the distance, and from the distance to get the time spent on the road, plus the time of getting on and off the boat, it is the total time.

C++ code 1:

#include<iostream>
#include<cmath>
using namespace std;
double ans,x,y,r;
int n;
int main()
{
    
    
	cin >> n;
	for(int i = 0; i < n; ++i)//读入的同时进行计算救援该批人所需要的时间,并计算在总时间之内
	{
    
    
		cin >> x >> y >> r;
		ans += sqrt(pow(x, 2) + pow(y, 2)) / 50 * 2 + r * 1.5;//该批人所用的时间是(x,y)与坐标原点直接的距离除以救生船的速度乘以2(一来一回)加上该批人上船下船的时间,上船:1.0*人数,下船:0.5*人数;总:1.5*人数
	}
	cout << ceil(ans) << "\n";//向上取整,ceil()是cmath头文件里面的函数,是对一个小数进行上取整
	return 0;
}

C++ code 2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n;
	while(cin >> n)
	{
    
    
		double sum = 0.0, x , y, r;
		for(int i = 0; i < n; i++)
		{
    
    
			cin >> x >> y >> r;
			sum += sqrt(x * x + y * y) * 1.0 * 2 / 50;
			sum += 1.5 * r;
		}
		cout << ceil(sum) << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113095662