分三类讨论:在15:00前(含15:00),在15:00后且19:00前(含19:00)和19:00后。计算时先转分钟,输出再转回去即可。
参考代码:
C语言:
#include <stdio.h>
int hh, mm, t1, t2;
int main()
{
scanf("%d%d", &hh, &mm);
t1 = hh * 60 + mm;
if (t1 <= 15 * 60) //在15:00前或15:00
{
t2 = 15 * 60 - t1;
}
else if (t1 <= 19 * 60) //在19:00前或19:00
{
t2 = 19 * 60 - t1;
}
else //在19:00后
{
t2 = 15 * 60 + (24 * 60 - t1);
}
printf("%d %d", t2 / 60, t2 % 60);
return 0;
}
Python:
hh, mm = [int(i) for i in input().strip().split(':')]
t1 = hh*60+mm
if t1 <= 15*60:
t2 = 15*60-t1
elif t1 <= 19*60:
t2 = 19*60-t1
else:
t2 = 15*60+(24*60-t1)
print('%d %d'%(t2/60, t2%60))