题目 668. 游戏时间2
读取四个整数 A,B,C,D,用来表示游戏的开始时间和结束时间。
其中 A 和 B 为开始时刻的小时和分钟数,C 和 D 为结束时刻的小时和分钟数。
请你计算游戏的持续时间。
比赛最短持续 1 分钟,最长持续 24 小时。
输入格式
共一行,包含四个整数 A,B,C,D。
输出格式
输出格式为 O JOGO DUROU X HORA(S) E Y MINUTO(S),表示游戏共持续了 X 小时 Y 分钟。
数据范围
0≤A,C≤23,
0≤B,D≤59
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
if (b > d)
{
c --;
d += 60;
}
if (a > c || a == c && b == d) c += 24;
cout << "O JOGO DUROU " << c - a << " HORA(S) E " << d - b << " MINUTO(S)" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int start = a * 60 + b;
int final = c * 60 + d;
int spend_time = final - start;
if (spend_time <= 0) spend_time += 1440;
cout << "O JOGO DUROU " << spend_time / 60 << " HORA(S) E " << spend_time % 60 << " MINUTO(S)" << endl;
return 0;
}
240218 判断结构 最笨办法分类讨论
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a < c && b <= d)
cout << "O JOGO DUROU " << c - a << " HORA(S) E " << d - b << " MINUTO(S)" << endl;
if (a < c && b > d) cout << "O JOGO DUROU " << c - a - 1 << " HORA(S) E " << d - b + 60 << " MINUTO(S)" << endl;
if (a == c && b < d) cout << "O JOGO DUROU " << c - a << " HORA(S) E " << d - b << " MINUTO(S)" << endl;
if (a == c && b == d) cout << "O JOGO DUROU " << c - a + 24 << " HORA(S) E " << d - b << " MINUTO(S)" << endl;
if (a == c && b > d) cout << "O JOGO DUROU " << c - a + 23 << " HORA(S) E " << d - b + 60 << " MINUTO(S)" << endl;
if (a > c && b <= d) cout << "O JOGO DUROU " << c - a + 24 << " HORA(S) E " << d - b << " MINUTO(S)" << endl;
if (a > c && b > d) cout << "O JOGO DUROU " << c - a + 23 << " HORA(S) E " << d - b + 60 << " MINUTO(S)" << endl;
return 0;
}