hdu 1861 游船出租 tag:模拟

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1861     一个研究生入学考试上机题==b

思路: 将信息放在结构体里,然后存在向量中, 最后扫描到n==0就统计,输出,清空向量。  

需要注意的是 1 同一艘船可能出租两次,于是扫描到op=='E' 时,就要立马处理。

                         2 扫描到 op=='E' 时 ,还要看是否有S的记录,如果没有找到就不处理

                         3 从00:00 -> 00:00 算是借了一分钟~   而不是差值0分钟

                         4 字符串和整数之间转化   用stringstream ,#include <sstream>

 代码:

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<sstream>
#include<cmath>
using  namespace std;

struct info
{
  int number;
  char op;
  string time;

};
int stringtoint(string s)
{
   int ans;
   stringstream ss;
   ss<<s;
   ss>>ans;
   return ans;


}
int main()
{

  int n;
  vector<info>  v;
  while(cin>>n)
  {
     info theinfo;
     char op;
     cin>>op;

     string time;
     cin>>time;


     if(n==-1)  break;
     if(n==0)
     {

       int count=0;
       double ave=0;

       map<int,string>   start;
       map<int,string>   end;

       for(int i=0;i<v.size();i++)
       {
           if(v[i].op=='S')
           start[v[i].number]=v[i].time;

           else   if(v[i].op=='E'&&start.find(v[i].number)!=start.end())
           {

              count++;
              string starthour=start.find(v[i].number)->second.substr(0,2);
              string startminute=start.find(v[i].number)->second.substr(3,2);
              string endhour=v[i].time.substr(0,2);
              string endminute=v[i].time.substr(3,2);


              // 分钟计算小心一点

              if(startminute<=endminute)
              ave+=stringtoint(endminute)-stringtoint(startminute);
              else
              {
               ave+=stringtoint(endminute)-stringtoint(startminute)+1;

              }

               ave+=(stringtoint(endhour)-stringtoint(starthour))*60;
           }

           }

       v.clear();


       if(count!=0)
       ave=ave/count;
       cout<<count<<" "<<floor(ave)<<endl;


       }


     else
     {

     theinfo.time=time;
     theinfo.number=n;
     theinfo.op=op;

     v.push_back(theinfo);

     }
  }
}

原文地址:https://www.cnblogs.com/814jingqi/p/3247187.html