题目链接(- ->点击题目链接自行查看)
解题思路如下:
1.如果均为上午或下午,且起床时间比入睡时间晚,则可以直接相减
否则起床时间 + 24再相减。
2.因为数字未0~9(无13、14...,本人就是看错了...)
此时起床时间 + 12和入睡时间相减即可。
贴一份超时代码1001ms。。。。。。,全超时,我也是醉了~~
#include <iostream>
#include <cstring>
using namespace std;
int starthour, startminute, endhour, endminute;
int hour, minute;
char chstart, chend;
void Large()
{
endminute += 60;
--endhour;
}
int TimeProblemAAPPAP(int H, int M)
{
if(startminute > endminute) Large();
H = endhour - starthour;
M = endminute - startminute;
if(H == hour && M == minute) printf("YES\n");
else
{
printf("NO\n");
printf("%dh%dmin", H, M);
}
}
int TimeProblemPA(int H, int M)
{
H = 11 - starthour;
M = 60 - startminute;
H += endhour;
M += endminute;
if(M >= 60)
{
M -= 60;
++H;
}
if(H == hour && M == minute) printf("YES\n");
else
{
printf("NO\n");
printf("%dh%dmin", H, M);
}
}
int main()
{
bool flag = false;
int H, M;
scanf("%d:%d %c.m", &starthour, &startminute, &chstart);
scanf("%d:%d %c.m", &endhour, &endminute, &chend);
scanf("%dh%dmin", &hour, &minute);
if((chstart == 'a' && chend == 'a') || (chstart == 'p' && chend == 'p') || (chstart == 'a' && chend == 'p'))
{
TimeProblemAAPPAP(H, M);
}
if(chstart == 'p' && chend == 'a')
{
TimeProblemPA(H, M);
}
return 0;
}
看到别人解后,自愧不如,短短几十行代码,就把所有情况都容纳进去~~
#include <iostream>
using namespace std;
int h1, h2, m1, m2, hh, mm, h, m;
char a[3], b[3];
int main()
{
scanf("%d:%d %s\n", &h1, &m1, a);
scanf("%d:%d %s\n", &h2, &m2, b);
scanf("%dh%dmin", &hh, &mm);
if(a[0] != b[0]) h2 += 12;
h = h2 - h1, m = m2 - m1;
if(m < 0) m += 60, --h;
if(h < 0) h += 24;
if(h == hh && m == mm) puts("YES");
else puts("NO"), cout << h << "h" << m << "min";
return 0;
}
有时是真的得多思考,总结才是。