题解 | #游船出租#

游船出租

http://www.nowcoder.com/practice/50fc5c0009cf4083bca5fbedcb4b6dc0

#include<iostream>
#include<string>
#include<vector>
using namespace std;

struct ship{
    int time;
    int isOk = 0;
};

int main(){
    vector<ship> str(100);
    str.resize(100);
    float num = 0, answer = 0;
    int a;
    while(scanf("%d ",&a)!=EOF){
        float hour,minute,time,period;
        char c;
        if(!(a>=0&&a<=100)) break;
        scanf("%c %f:%f",&c,&hour,&minute);
        time = hour*60+minute;
        //记录当前输入的数据

        //放到容器里面
        if(a!=0){
            if(c=='S') {
                //记录开始时间,并标记数据需要结束数据
                str[a].time = time;
                str[a].isOk = 1;
            }else if(str[a].isOk == 1){
                //如果有开始数据,记录结束数据
                num++;
                answer+=(time - str[a].time);
//                 str[a].time = 0;
//                 str[a].isOk = 0;
            }
        }else{
            if(num==0) cout<<"0 0"<<endl;
            else cout<<int(num)<<" "<<int(answer/num+0.5)<<endl;
            str.clear();
            str.resize(100);
            answer = 0;
            num = 0;
        }
    }

    return 0;
}

评测例子有问题,参考代码中注释掉的部分。
还船的时候必须要考虑该船是否处在借出去的状态,样例中借出1次还回来多次也算进去了。
添加注释后代码AC了,正常来说,应该把注释的两行去掉。

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务