PC/UVa 110404/10191 Longest Nap

 

比较简单的一道题,先按照开始时间排序,再将字符串转化为数字,用下一个的结束时间减去第一个的开始时间

注意

小时与分钟之间的转换,

10:00 与第一个工作时间的开始时间之间的差,和最后一个工作时间的结束时间与18:00之间的时间差都要计算在内

我的代码中sscanf()在头文件<cstdlib>中

用法 int  a,b;

sscanf("12:30","%d:%d",a,b);可以得到a=12 ; b=30;

其实sscanf()还有很多的用法,如果一次总结了我觉得还是会忘的,还是等到遇到题的时候再写吧

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char thing[300];
struct node
{
 char time1[10],time2[10];
};
int cmp(node a,node b)
{
 if(strcmp(a.time1,b.time1)>=0)
  return 0;
 return 1;
}
node tt[1005];
void fun(char a[],int h,int m,int k)
{
 if(h!=0)
 {
  cout<<"Day #"<<k<<": the longest nap starts at "<<a<<" and will last for "<<h<<" hours and "<<m<<" minutes."<<endl;
 }
 else
  cout<<"Day #"<<k<<": the longest nap starts at "<<a<<" and will last for "<<m<<" minutes."<<endl;
}
int main()
{
 int s[2],e[2],i;
 int n;
 int t;
 int h,m;
 int flag;
 int k=0;
 while(cin>>n)
 {
  k++;
  for(i=0;i<n;i++)
  {
   cin>>tt[i].time1>>tt[i].time2;
   cin.getline(thing,300);
  }
  sort(tt,tt+n,cmp);
  m=0;
  flag=-1;
  sscanf(tt[0].time1,"%d:%d",&s[0],&s[1]);
  m=(s[0]-10)*60+s[1];
  for(i=1;i<n;i++)
  {
   sscanf(tt[i].time1,"%d:%d",&s[0],&s[1]);
   sscanf(tt[i-1].time2,"%d:%d",&e[0],&e[1]);
   t=(s[0]-e[0])*60+s[1]-e[1];
   if(m<t)
   {
    m=t;
    flag=i-1;
   }
  }
  sscanf(tt[n-1].time2,"%d:%d",&e[0],&e[1]);
  t=(18-e[0])*60-e[1];
  if(t>m)
  {
   m=t;
   flag=n-1;
  }
  h=m/60;
  m=m%60;
  if(flag==-1)
   fun("10:00",h,m,k);
  else
   fun(tt[flag].time2,h,m,k);
 }
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值