2501 Average Speed

来源:互联网 发布:淘宝店铺名称设计 编辑:程序博客网 时间:2024/06/09 17:33
  1. /*
  2.   第一行的速度可能为0,故初始速度要为0
  3.   */
  4. #include<iostream>
  5. using namespace std;
  6. int main()
  7. {
  8.     freopen("in.txt","r",stdin);
  9.     int h,m,s,k(0);
  10.     int pres(0),nows,time;
  11.     double sum=0.0;
  12.     char c;
  13.     while(scanf("%d%c%d%c%d",&h,&c,&m,&c,&s)!=EOF)
  14.     {
  15.         c=getchar();
  16.         if(h==24)
  17.             h=0;
  18.         if(c!='/n')
  19.         {
  20.         
  21.             if(pres!=0)
  22.             {
  23.             nows=s+m*60+h*60*60;
  24.             time=nows-pres;
  25.             sum+=time*k/3600.0;
  26.             }
  27.                 cin>>k;
  28.         }
  29.         else
  30.         {
  31.         
  32.             nows=s+m*60+h*60*60;
  33.             time=nows-pres;
  34.             if(k!=0)
  35.             sum+=time*k/3600.0;
  36.             if(h<10)
  37.                 cout<<0<<h<<":";
  38.             else
  39.                 cout<<h<<":";
  40.             if(m<10)
  41.                 cout<<0<<m<<":";
  42.             else
  43.                 cout<<m<<":";
  44.             if(s<10)
  45.                 cout<<0<<s<<" ";
  46.             else
  47.                 cout<<s<<" ";
  48.             printf("%0.2lf km/n",sum);
  49.         }
  50.     
  51.         pres=s+m*60+h*60*60;
  52.     
  53.     }
  54.     return 0;
  55. }
关键是输出格式的处理,还又要注意 24小时变为0小时
原创粉丝点击