UVa——110404 Longest Nap (排序)
解题思路:题意很明确,就是依据日程安排找出最长的可能打盹的时间。
其实就是把所有空闲的时间排序(先要对日程安排时间从小到大排序,然后计算出可能的打盹时间),然后找到最长的即为所求。需要注意的是:时间从 10:00 开始,到 18:00 结束,在计算过程中一定要把边界考虑上。我定义了两个结构体,其中 struct Time 记录的是日程安排的开始时间、结束时间;而 struct Break 记录的是可以打盹的开始时间和结束时间,还有可以打盹的时间。按照时间的格式,所以选择用字符数组来保存时间,而日程安排事项自然也用字符数组,注意包含空格,故选用 gets() 输入。在计算打盹时间时是按分钟计算的。由于思路明确,所以一次就AC了,有点小小的成就感,因为做挑战编程上的题几乎没有一次通过的。
View Code
1 #include<iostream>
2 #include<algorithm>
3 #include<cstring>
4 #include<cstdio>
5 using namespace std;
6
7 struct Time{ //日程安排
8 char st[6];//开始
9 char et[6];//结束
10 }t[105];
11 struct Break{ //打盹
12 char start[6]; //开始
13 char end[6]; //结束
14 int bt;
15 }b[105];
16
17 int cmp1(Time a,Time b) //日程安排排序
18 {
19 return strcmp(a.st,b.st)<0;
20 }
21 bool cmp2(Break a,Break b) //打盹时间排序
22 {
23 return a.bt>b.bt;
24 }
25
26 int main()
27 {
28 int n,i,k=0;
29 char str[260];
30 while(cin>>n)
31 {
32 k++;
33 for(i=0;i<n;i++)
34 {
35 cin>>t[i].st>>t[i].et;
36 gets(str); //注意含有空格
37 }
38 sort(t,t+n,cmp1);
39 strcpy(b[0].start,"10:00");
40 strcpy(b[n].end,"18:00");
41 for(i=0;i<=n;i++) //计算打盹时间
42 {
43 if(i!=0) strcpy(b[i].start,t[i-1].et);
44 if(i!=n) strcpy(b[i].end,t[i].st);
45 b[i].bt=(b[i].end[0]-'0')*600+(b[i].end[1]-'0')*60+
46 (b[i].end[3]-'0')*10+(b[i].end[4]-'0')-
47 ((b[i].start[0]-'0')*600+(b[i].start[1]-'0')*60+
48 (b[i].start[3]-'0')*10+(b[i].start[4]-'0'));
49 }
50 sort(b,b+n+1,cmp2);
51 if(b[0].bt<60)
52 cout<<"Day #"<<k<<": the longest nap starts at "<<
53 b[0].start<<" and will last for "<<b[0].bt<<" minutes."<<endl;
54 else cout<<"Day #"<<k<<": the longest nap starts at "<<b[0].start<<
55 " and will last for "<<b[0].bt/60<<" hours and "<<b[0].bt%60<<" minutes."<<endl;
56 }
57 return 0;
58 }
2 #include<algorithm>
3 #include<cstring>
4 #include<cstdio>
5 using namespace std;
6
7 struct Time{ //日程安排
8 char st[6];//开始
9 char et[6];//结束
10 }t[105];
11 struct Break{ //打盹
12 char start[6]; //开始
13 char end[6]; //结束
14 int bt;
15 }b[105];
16
17 int cmp1(Time a,Time b) //日程安排排序
18 {
19 return strcmp(a.st,b.st)<0;
20 }
21 bool cmp2(Break a,Break b) //打盹时间排序
22 {
23 return a.bt>b.bt;
24 }
25
26 int main()
27 {
28 int n,i,k=0;
29 char str[260];
30 while(cin>>n)
31 {
32 k++;
33 for(i=0;i<n;i++)
34 {
35 cin>>t[i].st>>t[i].et;
36 gets(str); //注意含有空格
37 }
38 sort(t,t+n,cmp1);
39 strcpy(b[0].start,"10:00");
40 strcpy(b[n].end,"18:00");
41 for(i=0;i<=n;i++) //计算打盹时间
42 {
43 if(i!=0) strcpy(b[i].start,t[i-1].et);
44 if(i!=n) strcpy(b[i].end,t[i].st);
45 b[i].bt=(b[i].end[0]-'0')*600+(b[i].end[1]-'0')*60+
46 (b[i].end[3]-'0')*10+(b[i].end[4]-'0')-
47 ((b[i].start[0]-'0')*600+(b[i].start[1]-'0')*60+
48 (b[i].start[3]-'0')*10+(b[i].start[4]-'0'));
49 }
50 sort(b,b+n+1,cmp2);
51 if(b[0].bt<60)
52 cout<<"Day #"<<k<<": the longest nap starts at "<<
53 b[0].start<<" and will last for "<<b[0].bt<<" minutes."<<endl;
54 else cout<<"Day #"<<k<<": the longest nap starts at "<<b[0].start<<
55 " and will last for "<<b[0].bt/60<<" hours and "<<b[0].bt%60<<" minutes."<<endl;
56 }
57 return 0;
58 }