|
发表于 2023-10-13 18:48:37
|
显示全部楼层
这个问题可以通过比较时间来解决。首先,我们需要将输入的时间转换为秒,然后与预设的时间段进行比较。以下是一个使用C++编写的示例代码:
- #include <iostream>
- #include <string>
- int main() {
- std::string time;
- std::cin >> time;
- int hour = std::stoi(time.substr(0, 2));
- int minute = std::stoi(time.substr(3, 2));
- int second = std::stoi(time.substr(6, 2));
- int total_seconds = hour * 3600 + minute * 60 + second;
- int morning_start = 6 * 3600;
- int morning_end = 7 * 3600;
- int noon_start = 12 * 3600;
- int noon_end = 13 * 3600;
- int evening_start = 17 * 3600;
- int evening_end = 18 * 3600;
- int day_end = 18 * 3600;
- int day_start = 6 * 3600;
- if (total_seconds < day_start || total_seconds >= day_end) {
- std::cout << "resting";
- } else if ((total_seconds >= morning_start && total_seconds < morning_end) ||
- (total_seconds >= noon_start && total_seconds < noon_end) ||
- (total_seconds >= evening_start && total_seconds < evening_end)) {
- if (total_seconds % 3600 == 0) {
- std::cout << "on the way";
- } else {
- std::cout << "eating";
- }
- } else {
- std::cout << "studying";
- }
- return 0;
- }
复制代码
这段代码首先读取输入的时间,并将其转换为小时、分钟和秒。然后,它将这些时间单位转换为总秒数。接着,它定义了早餐、午餐和晚餐的开始和结束时间,以及一天的开始和结束时间。
然后,代码检查当前时间是否在一天的开始和结束时间之外。如果是,那么输出\"resting\"。如果不是,它会检查当前时间是否在早餐、午餐或晚餐的时间段内。如果是,并且当前时间正好是小时的开始(即分钟和秒都为0),那么输出\"on the way\"。否则,输出\"eating\"。如果当前时间不在任何已定义的时间段内,那么输出\"studying\"。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 |
|