Ride to Office

描述
Many staff of are living in a place called MZone, far from their office( 4.5 km ). Due to the bad traffic, many staff choose to ride a bike.

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

We assume the time that Weiwei gets to the gate of MZone is zero. Given the set off time and speed of the other people, your task is to give the time when Weiwei arrives at his office.

输入
There are several test cases. The first line of each case is N (1 <= N <= 10000) representing the number of riders (excluding Weiwei). 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 one line for each case: the arrival time of Weiwei. Round up (ceiling) the value when dealing with a fraction.
样例输入
4
20 0
25 -155
27 190
30 240
2
21 0
22 34
0

样例输出
780
771
拿题思路:首先他是要求解最短的时间去到办公室,而且每次要跟着一个人,而且后面有人超过他,他就得跟着更快的那个,从这里读解到最后到达时他跟的人一定是他遇到的最快的,而且他的时间和遇到的最快的那个的时间加上那个人出发的时间;得出以下几点:
1)最终到达时一定是和他遇到的最快的一个;
2)最终时间为遇到最快的那个骑行时间加上出发间隔的时间;
3)一定不会跟比他先出发那个,应为快了追不上,追上了又没必要跟;
遇到问题
1)首先便是题目的翻译;
2)其次数组给小了导致runtime error;
3)最后在速度转换时忽略了5/18是一个整数,导致结果错误;
解题步骤
1)定义一个结构体用来储存每一个人的速度和出发时间;
2)计算他分别跟每个人一起时的时间(提前出发的除外);
3)找出时间最短的那个,即为所需结果;

#include <iostream>
#include <cmath>
using namespace std;
struct qs                   //结构体储存每个人的速度和出发时间;
{
    int v;
    int t;
}a[10001];
int main()
{
    int n,i;
    double b[10001],min;
    while(cin>>n&&n!=0)
        {
            min=99999.0;
            for(i=0;i<n;i++)
                {
                    cin>>a[i].v>>a[i].t;
                    if(a[i].t>=0)
                        {
                            b[i]=3.6*4500/a[i].v+a[i].t;   //计算分别跟每个人的时间;
                            if(min>b[i])            //找出最小值;
                                min=b[i];
                        }
                    else continue;
                }
            cout<<ceil(min)<<endl;
        }
    return 0;
}

感想:通过这个题我感觉在有一些情况贪心思想就是有关数学题的求解似的,还有就是自己的细节问题考虑的不是周到,导致犯错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值