B - Shout Everyday 解説 by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
The problem can be solved by doing casework depending on whether he is asleep at midnight
[1] If he is not asleep at midnight
Within a day, He goes to bed and wakes up, so \(B<C\). Since he sleeps from \(B\) to \(C\) o’clock, the answer is No
if \(A\) o’clock is in between, i.e. if \(B<A<C\). Otherwise the answer is Yes
.
[2] If he is asleep at midnight
Within a day, he first wakes up, then goes to bed, so \(C<B\). Since he is awake from \(C\) to \(B\) o’clock, the answer is YEs
if \(A\) o’clock is in between, i.e. if \(C<A<B\). Otherwise the answer is No
.
Sample code (Python)
A, B, C = map(int, input().split())
if B < C:
if B < A < C:
print("No")
else:
print("Yes")
else:
if C < A < B:
print("Yes")
else:
print("No")
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int A, B, C;
cin >> A >> B >> C;
if (B < C)
{
if (B < A && A < C)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
else
{
if (C < A && A < B)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
In a problem with a lot of casework like this, it is sometimes effective to adopt the way that does not require a casework to avoid penalties due to oversight or mistakes in caseworks.
In this problem, one can use a while statement to avoid caseworks. Starting from \(B\) o’clock, increment the current hour of day one by one. If \(A\) o’clock comes before becoming \(C\) o’clock, then print No
; otherwise, print Yes
.
Sample code (Python)
A, B, C = map(int, input().split())
now = B
while now != C:
if now == A:
print("No")
exit()
now += 1
now %= 24
print("Yes")
投稿日時:
最終更新: