1922 Ride to School

探讨了北京大学万柳校区学生Charley的特殊骑行习惯及其对到达燕园时间的影响,通过分析其他学生出发时间和速度,计算Charley到达目的地的最短时间。

Description

Many graduate students of Peking University are living in Wanliu Campus, which is 4.5 kilometers from the main campus – Yanyuan. Students in Wanliu have to either take a bus or ride a bike to go to school. Due to the bad traffic in Beijing, many students choose to ride a bike.

We may assume that all the students except “Charley” ride from Wanliu to Yanyuan at a fixed speed. Charley is a student with a different riding habit – he always tries to follow another rider to avoid riding alone. When Charley gets to the gate of Wanliu, he will look for someone who is setting off to Yanyuan. If he finds someone, he will follow that rider, or if not, he will wait for someone to follow. On the way from Wanliu to Yanyuan, at any time if a faster student surpassed Charley, he will leave the rider he is following and speed up to follow the faster one.

We assume the time that Charley gets to the gate of Wanliu is zero. Given the set off time and speed of the other students, your task is to give the time when Charley arrives at Yanyuan.
Input

There are several test cases. The first line of each case is N (1 <= N <= 10000) representing the number of riders (excluding Charley). N = 0 ends the input. The following N lines are information of N different riders, in such format:

Vi [TAB] Ti

Vi is a positive integer <= 40, indicating the speed of the i-th rider (kph, kilometers per hour). Ti is the set off time of the i-th rider, which is an integer and counted in seconds. In any case it is assured that there always exists a nonnegative Ti.
Output

Output one line for each case: the arrival time of Charley. Round up (ceiling) the value when dealing with a fraction.
Sample Input

4
20 0
25 -155
27 190
30 240
2
21 0
22 34
0
Sample Output

780
771

我想着应该是一道简单的数学题…然后我就卡在了第二个测试的771上
错误代码1:

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int n,pub=16200;
	while(cin>>n){
		if(n==0) break;
		int time=999999,t,v;
		for(int i=0;i<n;i++){
			cin>>v>>t;
			if(t>=0){
				t=(pub/v)+t;
				if(t<=time){
					time=t;
				}
			}
		}
		cout<<time<<endl;
	}
	return 0;
}

运算结果770

错误代码2

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
	int n,pub=16200;
	while(cin>>n){
		if(n==0) break;
		double time=999999,t,v;
		for(int i=0;i<n;i++){
			cin>>v>>t;
			if(t>=0){
				t=(pub/v)+t;
				if(t<=time){
					time=t;
				}
			}
		}
		printf("%.0lf\n",time);
	}
	return 0;
}

运算结果770
我寻思着这就是一个四舍五入的问题啊???
终究是我还太菜…

依旧是学习大佬的一天。

今天新学习了一个方法:

函数名: ceil

用 法: double ceil(double x);

功 能: 返回大于或者等于指定表达式的最小整数

头文件:math.h

返回数据类型:double

AC代码:

 #include<iostream>
#include<cmath>
using namespace std;
int main(){
	int n;
	double pub=16200;
	while(cin>>n&&n){
		int v,t;
		double time=999999,temp;
		for(int i=0;i<n;i++){
			cin>>v>>t;
			if(t>=0){
				temp=(pub/v)+t;
				if(temp<=time){
					time=temp;
				}
			}
		}
		cout<<ceil(time)<<endl;
	}
	return 0;
}

啊啊啊啊啊!!!麻麻我终于通过了!
今天又是学习的一天呢!总有一天我会进步的!

### 问题解析 题目要求解决一个关于骑车人到达办公室时间的问题。每位骑车人的速度以 km/h 为单位,出发时间以秒为单位给出。目标是找出最早到达办公室的时间。 根据题目描述,骑车人只有在出发时间 $ T_i \geq 0 $ 的情况下才可能被考虑。每位骑车人的骑行距离为 4.5 公里,因此需要将速度转换为 m/s,再计算所需时间。最终到达时间等于出发时间加上骑行时间,骑行时间向上取整[^1]。 ### C++ 实现思路 代码逻辑如下: 1. 读取输入数据,直到遇到 `n == 0` 为止。 2. 对于每个骑车人,仅考虑出发时间 $ T_i \geq 0 $ 的情况。 3. 计算骑行时间:由于速度单位是 km/h,需要将其转换为 m/s,即 $ \text{speed (m/s)} = \frac{\text{speed (km/h)}}{3.6} $。 4. 骑行时间 $ t_{\text{ride}} $ 为 $ \frac{4500}{v} $,向上取整后加上出发时间 $ T_i $。 5. 找出所有骑车人中最早到达的时间,并输出。 ### C++ 代码实现 ```cpp #include <iostream> #include <cmath> using namespace std; int main() { int n; while (cin >> n && n != 0) { int min_time = 100000000; // 初始化为一个极大值 for (int i = 0; i < n; ++i) { int v, t; cin >> v >> t; if (t >= 0) { double ride_time = 4500.0 / v; // 计算骑行时间(秒) int total_time = static_cast<int>(ceil(ride_time)) + t; if (total_time < min_time) { min_time = total_time; } } } cout << min_time << endl; } return 0; } ``` ### 代码说明 - 使用 `ceil` 函数确保骑行时间向上取整,因为骑车人即使在最后一秒也要计入完整的时间单位[^1]。 - `min_time` 初始化为一个极大值,用于记录最早到达的时间。 - 输入数据中包含多个测试用例,每个用例以 `n` 开头,直到 `n == 0` 为止。 ### 输入输出示例 **输入样例**: ``` 4 20 0 25 -155 27 190 30 240 2 21 0 22 34 0 ``` **输出样例**: ``` 780 771 ``` ### 优化与扩展 - 可以将骑车人信息存储在结构体中,便于后续处理。 - 如果需要处理更多复杂情况(如多人同时到达),可以进一步扩展逻辑。 - 代码中使用 `double` 类型处理骑行时间,避免精度丢失。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值