为什么我的代码常含BUG ,因为我的写法乱的深沉

我最近写的代码出现了很多BUG ,为什么呢,大概是因为很多小细节引起的吧,
比如说:什么要求输入多组数据,读到文件结尾,我只写一组啊,诸如此类,尤其难找
我常常在想,为什么怎么想都对的代码会一直都WA,现在我明白了,一方面,这个和写代码的经验有关
如果一个人,拥有很多的经验,那么他就会明白,什么地方容易错,相反相成,就会慢慢的把代码写的准确而不易犯错
同时也与一个人的性格有关,有的人太不注重细节,往往会引起,很多的不必要的麻烦,花大量的时间去研究到底哪里有问题
但我觉得就我目前而言,每一次研究都是为了以后不再犯错,这样的错还是有意义的。

看看之前的一个题目:
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

思路就是找到0s之后出发的最早到达的人

#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
    int n,res;
    while(cin>>n&&n){
        res=inf;
        int i,t,t2;
        double v;
        for(i=1;i<=n;i++){
            cin>>v>>t;
            if(t>=0){
            v=(1.0*v/3.6);
            t2=ceil(4500/v);
            res=min(res,t+t2);
            }
        }
        cout<<res<<endl;
    }

    return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
    int n,res;
    while(cin>>n&&n){
        res=inf;
        int i,v,t;
        double t2;
        for(i=1;i<=n;i++){
            cin>>v>>t;
            if(t>=0){
            t2=4.5/v*3600;
            t=ceil(t2)+t;
            res=min(res,t);
            }
        }
        cout<<res<<endl;
    }

    return 0;
}

这两段代码的差别就在于关于求时长的表达式,前者先除3.6 得到的结果经过一次保留,又用4500除以保留的结果,导致结果不精确,用了CEIL以后即使恰好是整数也会取整+1;后者用*3600 就没有这个问题,这件事告诉我一个道理,能乘千万不要除,以保证程序中的数据的完整性

全部评论

相关推荐

Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务